【发布时间】:2019-09-09 05:35:14
【问题描述】:
我有以下设置:BaseVC(里面的容器视图)-connectedTo-cmetsVC(里面是一个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 调出 cmetsVC,然后获取应该放入其中的数据:
@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? 我看过 here 和 here 但都没有帮助。
我已经尝试过:
func loadComments(completion: @escaping(()->())) {
并在 firebase func 的末尾调用,例如: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()
})
}
}
【问题讨论】:
标签: ios swift firebase uitableview