gpt4 book ai didi

ios - 如何在 swift iOS Firestore 中获取帖子单元格 documentId?

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

下面是我正在处理的代码,有用于评论列表的表格 View 和表格单元格,当我单击单元格中的按钮时,如何从 firestore 获取单元格的文档 ID。它没有显示错误,但它没有按要求执行,基本上单击后我想检索评论 ID 和帖子 ID,正在检索帖子 ID,但每个评论的 commentId 都是相同的,这一定是每个评论的差异。

我收到此错误消息“'NSInvalidArgumentException',原因:'-[UITableViewCellContentView addTarget:action:forControlEvents:]:无法识别的选择器发送到实例 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)
}
}

最佳答案

试试这个,让我知道它是否有效。制作这样的按钮

class CommentCell: UITableViewCell {

@IBOutlet weak var likeButton: UIButton!

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


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


@IBAction func likeButtonTapped(_ sender: UIButton) {

likeButtonPressed()
}


}

以及 UITableView cellForRowAt 方法内部

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


}

关于ios - 如何在 swift iOS Firestore 中获取帖子单元格 documentId?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58022597/

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