gpt4 book ai didi

ios - 如何在containerView中重新加载tableView?

转载 作者:行者123 更新时间:2023-11-29 05:50:09 24 4
gpt4 key购买 nike

我有以下设置:BaseVC(内部容器 View )-connectedTo-commentsVC(这里的内部是一个 tableView,它将填充自定义 commentCells)。

在加载 baseVC 后,它会被调用:

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if let VC = segue.destination as? CommentsVC {

VC.selectedMedia = selectedPost?.media[numberMedia]
VC.commentCellDelegate = self
VC.parentView = commentsContainer
commentsVC1 = VC
VC.post = selectedPost!
}

}

然后,当在 BaseVC 中按下按钮时,就会调用它,用 tableView 调出 commentsVC,然后获取应包含在其中的数据:

@objc func CommentsTapped(_ tap: UITapGestureRecognizer) {
//Bring up the comments view and load all data into it.
UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.9, options: .curveEaseOut, animations: {
print(self.commentsVC1.commentCellDelegate!)
self.commentsVC1.commentCellDelegate!.updateCommentSheet(frame: self.initalFrame.offsetBy(dx: 0, dy: 180))
self.loadComments()
//I tried calling self.commentsVC1.tableView.reloadData() here but nothing happens
})
}

然后在 BaseVC 类的底部,我有一个扩展,其中包含 tableView 的所有方法:

extension Phase3VC: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if arrayOfComments.count == 0 {
return 1
} else {
return arrayOfComments.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = commentTableViewCell()
do {
print("jdkhfjkfhasjklfhds")
let url = URL(string: (arrayOfComments[indexPath.row].user.profileImageUrlString)!)
let imageData = try Data(contentsOf: url!)
let image = UIImage(data: imageData)

cell.dateOfComment.text = arrayOfComments[indexPath.row].date
cell.commentText.text = arrayOfComments[indexPath.row].commentText
cell.profImage.image = image
cell.username.text = arrayOfComments[indexPath.row].user.username
} catch {


print(error, ": Failed in do block converting image")
}

return cell
}

}

所以我的问题是如何重新加载tableView以便将数据放入tableView?我已经查看了herehere但都没有帮助。

我尝试过:

func loadComments(completion: @escaping(()->())) {

并在 firebase 函数末尾调用,例如:completion()(请注意它是 .childAdded)

然后执行:

self.loadComments {
self.commentsVC1.tableView.reloadData()
}

但是没有成功

更新:

这是获取函数:

    func loadComments(completion: @escaping(()->())) {
print("Fetch comments")

let ref = Database.database().reference()
self.arrayOfComments.removeAll()
ref.child("Comments").child(selectedPost!.user.userID!).child(selectedPost!.media[0].postID!).child("\(numberImage+1)").observe(.childAdded) { (snap) in
let commentID = snap.key
let uid = snap.childSnapshot(forPath: "UID").value as! String
ref.child("users2").child(uid).observe(.value, with: { (snapshot) in
let username = snapshot.childSnapshot(forPath: "username").value
let profileImage = snapshot.childSnapshot(forPath: "profileImage").value

let newUser = User(theuserID: uid, theUsername: username as! String, theprofImage: profileImage as! String)

let commentText = snap.childSnapshot(forPath: "Comment").value!
let timeStamp = snap.childSnapshot(forPath: "timeStamp").value!
print(timeStamp, "This is the timeStamp")
let date = ConvertDate(mediaTimestamp: timeStamp as! Double, isItForP3: false).getDate!

let newComment = Comments(newUser: newUser, text: commentText as! String, timeStamp: date, NcommentID: commentID)
self.arrayOfComments.append(newComment)
print(self.arrayOfComments.count, ": comments added")
self.commentsVC1.tableView.reloadData()

completion()
})
}
}

最佳答案

loadComments 是异步的,所以

self.loadComments {
self.commentsVC1.tableView.reloadData()
}

因此在获取评论之前调用 reloadData


// it would be like this 
func loadComments(completion:@escaping(()-())) {
Api.getcomments {
// alter the dataSource array of the table
// either reload the table here or do the below line
completions()
}
}

关于ios - 如何在containerView中重新加载tableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55753511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com