gpt4 book ai didi

ios - Swift Firebase Like Post in Table View

转载 作者:行者123 更新时间:2023-11-28 07:56:49 25 4
gpt4 key购买 nike

我正在使用 Firebase 在 Swift 中实现一个问答程序。我想在我的 tableViewCell 中有一个喜欢的按钮。但是,我遇到了问题,因为帖子的数据在 tableView 类中,我可以在 tableViewCell 类中的 like 按钮上进行更改。我需要在这两者之间进行数据传输。谁能帮我解决这个问题?

如果它能帮助您理解我的问题,请在我的 TableView 中编写代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let answerCell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell") as! AnswerCell

let answer = answers[indexPath.row]

answerCell.answerText.text = answer.answerText
answerCell.username.text = "~ \(answer.username)"
answerCell.numberOfLikes.text = "\(answer.numberOflikes) liked this answer."

answerCell.answerText.numberOfLines = 0

return answerCell
}

我想在我的表格 View 单元格中有这样的代码。但是,我无法访问答案对象的数据。

 @IBAction func likeButtonClicked(_ sender: UIButton) {
ref = Database.database().reference()

ref.child("answerLikes").child((answer.id).observeSingleEvent(of: .value, with: {
(snapshot) in
if let _ = snapshot.value as? NSNull {
sender.setImage(UIImage(named: "filledHeart.png"), for: .normal)
} else {
answerLikes = [Auth.auth().currentUser?.uid : true]
ref.child("answerLikes").child(answer.id).updateChildValues(answerLikes)
sender.setImage(UIImage(named: "emptyHeart.png"), for: .normal)
}
})
}

最佳答案

已更新

这是您询问“我无法访问答案对象的数据”的解决方案

您可以在 answerCell 中创建一个接收答案对象的方法。

func recieveAnswerObject(answer : Answer){
// here you will get answer object
// here i am assuming Answer is your model class
}

//现在是时候从您的主 UIViewController 类调用它来发送答案对象了

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let answerCell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell") as! AnswerCell

let answer = answers[indexPath.row]
// from here we send answer object
answerCell.recieveAnswerObject(answer)
answerCell.answerText.text = answer.answerText
answerCell.username.text = "~ \(answer.username)"
answerCell.numberOfLikes.text = "\(answer.numberOflikes) liked this answer."

answerCell.answerText.numberOfLines = 0

return answerCell
}

现在是您询问如何在这两个类之间传输数据的部分

第一种实现方式

1.在 AnswerCell 类中创建一个委托(delegate),并向您编写 tableView 代码的 Controller 确认它。

2.单击按钮时触发委托(delegate),您将在 mainViewController 中获得回调。

第二种实现方式

1.在 AnswerCell 类中为 likeButtonClicked 创建一个 IBOulet。

2.不要创建 IBAction。

3.你的代码看起来像这样

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let answerCell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell") as! AnswerCell
// action added to button
answerCell.likeButtonClicked.addTarget(self, action: #selector(likeButtonClicked), for: .touchUpInside)
// updated set tag to button
answerCell.likeButtonClicked.tag = indexPath.row
let answer = answers[indexPath.row]
answerCell.answerText.text = answer.answerText
answerCell.username.text = "~ \(answer.username)"
answerCell.numberOfLikes.text = "\(answer.numberOflikes) liked this answer."

answerCell.answerText.numberOfLines = 0

return answerCell
}

并在你的 tableview 类中编写下面的代码

 @IBAction func likeButtonClicked(_ sender: UIButton) {
//updated now tag is row position of button in cell and is equal to (indexPath.row) of that particular button.tag is equal to
let tag = sender.tag
// here indexPath.row can be replace by tag so
//let answer = answers[indexPath.row] == let answer = answers[tag]
// updated till here
ref = Database.database().reference()

ref.child("answerLikes").child((answer.id).observeSingleEvent(of: .value, with: {
(snapshot) in
if let _ = snapshot.value as? NSNull {
sender.setImage(UIImage(named: "filledHeart.png"), for: .normal)
} else {
answerLikes = [Auth.auth().currentUser?.uid : true]
ref.child("answerLikes").child(answer.id).updateChildValues(answerLikes)
sender.setImage(UIImage(named: "emptyHeart.png"), for: .normal)
}
})
}

关于ios - Swift Firebase Like Post in Table View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47851015/

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