简体   繁体   中英

How to get the post cell documentId in swift iOS Firestore?

Below is the code I am working on, there are tableview and tablecells for the list of the comments, How Do I get the document id of the cell from firestore when I click on the button in the cell. It is not showing error, but it is not performing as required, basically upon click I want to retrieve both comment Id and Post Id, Post Id is being retrieved, but commentId for everycomment is same, that must be the difference for every comment.

I am getting this error message "'NSInvalidArgumentException', reason: '-[UITableViewCellContentView addTarget:action:forControlEvents:]: unrecognized selector sent to instance 0x7fc1a1659400'".

class CommentListViewController: UITableViewController {
    var comments = [Comment]()
    var postCommentList:Post?
    var postId:String = ""
    var commentKey:String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.isTranslucent = false
        getComments()
    }

    func getComments() {
        let commentsRef = Firestore.firestore().collection("posts").document(postId).collection("comments")
        commentsRef.getDocuments { (snapshot, error) in
            if let error = error {
                print(error.localizedDescription)

            } else {
                if let snapshot = snapshot {
                    for document in snapshot.documents {
                        let data = document.data()
                        let username = data["comment_author_username"] as? String ?? ""
                        let comment = data["comment_author_comment"] as? String ?? ""
                        let spinnerC = data["comment_author_spinnerC"] as? String ?? ""
                        let fullname = data["comment_author_fullname"] as? String ?? ""
                        let email = data["comment_author_email"] as? String ?? ""
                        let commentUserImageUrl = data["comment_user_image"] as? String ?? ""
                        let newComment = Comment(_documentId: document.documentID, _commentAuthorUsername: username, _commentAuthorFullName: fullname, _commentAuthorComment: comment, _commentUserImage: commentUserImageUrl, _commentAuthorSpinnerC: spinnerC)
                        self.comments.append(newComment)

                    }
                    self.tableView.reloadData()
                }
            }
        }
    }

    @IBAction func toAddCommentsSection(_ sender: Any) {
        performSegue(withIdentifier: "toPostComment", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc = segue.destination as! CommentPostViewController
        vc.postId2 = postId
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return comments.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell
        cell.likebutton.tag = indexPath.row
        cell.likebutton.addTarget(self, action: #selector(likeaction1(_:)), for: .touchUpInside)
        cell.set(comment: comments[indexPath.row])

        return cell
    }

    @IBAction func likeaction1(_ sender: AnyObject) {
        let commentbutton = sender as! UIButton
        let comment = comments[commentbutton.tag]
        commentKey = comment._documentId // or what key value it is
        print(postId + "   hello1  " + commentKey)
    }
}

Try this and let me know it is working or not. Make button like this

class CommentCell: UITableViewCell {

@IBOutlet weak var likeButton: UIButton!

var likeButtonPressed : (() -> ()) = {}


override func awakeFromNib() {
    super.awakeFromNib()
}


@IBAction func likeButtonTapped(_ sender: UIButton) {

    likeButtonPressed()
}


}

And inside UITableView cellForRowAt method

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell

cell.likeButtonPressed = {
    commentKey = comments._documentId[indexPath.row]

}

return cell


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM