gpt4 book ai didi

swift - 如何从 Firestore 中检索值以更改每个监听项的表格单元格中按钮的文本?

转载 作者:行者123 更新时间:2023-11-28 05:36:38 26 4
gpt4 key购买 nike

我有一个 tableview,其中有列表项,每个单元格都有一个按钮,我想根据从 firestore 检索到的 bool 值来修复按钮的文本和颜色,每个单元格的按钮都有特定的文本和颜色基于只有值,我知道在 swift 中为按钮设置文本和颜色的语法,我只是不能根据 firestore 的值来做,下面是代码

获取列表的代码,关注的值为checkl1value

func getComments() {

//print(postId + "received")
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 commentuser_id = data["comment_author_id"] as? String ?? ""
self.checkl1value = data["l1"] as? DarwinBoolean


let newComment = Comment(_documentId: document.documentID, _commentAuthorUsername: username, _commentAuthorFullName: fullname, _commentAuthorComment: comment, _commentUserImage: commentUserImageUrl, _commentAuthorSpinnerC: spinnerC, _commentAuthorId:commentuser_id )
self.comments.append(newComment)

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

cellForRowAt 代码

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


cell.commentLikebutton.tag = indexPath.row
cell.commentLikebutton.addTarget(self, action: #selector(likeaction1(_:)), for: .touchUpInside)


if checkl1value == true {
cell.commentLikebutton.setTitle("Text1", for: .normal)
// cell.commentLikebutton.backgroundColor = UIColor(red: 17.0/255.0, green: 119.0/255.0, blue: 151.0/255.0, alpha: 1.0)
cell.commentLikebutton.backgroundColor = UIColor.red
//cell.commentLikebutton.backgroundColor = UIColor?.red()
}
else{
cell.commentLikebutton.setTitle("Text2", for: .normal)

}


cell.set(comment: comments[indexPath.row])

return cell


}

最佳答案

cellForRowAt的顶部添加这样的东西

let thisComment = self.comments[indexPath.row]
let checkl1value = thisComment.checkl1value

关于swift - 如何从 Firestore 中检索值以更改每个监听项的表格单元格中按钮的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58433697/

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