【发布时间】:2019-09-20 06:04:13
【问题描述】:
下面是我正在处理的代码,有用于 cmets 列表的 tableview 和 tablecells,当我单击单元格中的按钮时,如何从 firestore 获取单元格的文档 ID。它没有显示错误,但没有按要求执行,基本上在单击时我想同时检索评论 ID 和帖子 ID,正在检索帖子 ID,但每个评论的评论 ID 相同,这必须是每个评论的差异。
我收到此错误消息“'NSInvalidArgumentException',原因:'-[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)
}
}
【问题讨论】:
-
你的likeaction1按钮在tableView单元格类中吗
-
是的,你是对的
-
那么问题就在这里
-
stackoverflow.com/questions/39947076/…。 ...检查此答案以了解按钮操作
-
@Wings 我提到了这个问题,但它没有帮助,它仍然给我同样的错误问题,请帮助
标签: ios swift firebase google-cloud-firestore