如何在容器视图内重新加载表视图?

时间:2019-04-18 20:45:09

标签: ios swift firebase uitableview

我具有以下设置:BaseVC(内部容器视图)-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中按下按钮时,将调用该方法,将tableTableView调出commentVC,然后获取其中应包含的数据:

@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()
        })
    }
}

1 个答案:

答案 0 :(得分:1)

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()
   }
 }