【问题标题】:Swift Firebase Like Post in Table ViewSwift Firebase 类似于表格视图中的帖子
【发布时间】:2017-12-16 23:48:50
【问题描述】:

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

如果它可以帮助您理解我的问题,请在我的表格视图中编写代码:

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
    }

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

 @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)
        }
    })
}

【问题讨论】:

  • 方法likeButtonClicked放在哪里?
  • 它在 AnswerCell 中。它应该在表格视图控制器中吗?感谢您的回复。

标签: ios swift firebase firebase-realtime-database firebase-authentication


【解决方案1】:

更新

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

您可以在 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 类中创建一个委托,并在您编写 tableView 代码的控制器中确认它。

2.当按钮被点击时触发一个委托,你将在你的 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)
    }
})
   }

【讨论】:

  • 感谢您的回复,但在要在表格视图中创建的 likeButtonClicked 操作中,我无法编写“child(answer.id)”,因为我没有答案。我保留答案列表并在表格视图函数中以 answers[indexPath.row] 的形式访问单个答案。请问有什么帮助吗?
  • 您可以查看我的更新答案。我已在我进行更改的地方标记为已更新。希望您能得到答案。如果遇到上述解决方案的任何问题,请告诉我
  • 我得到了答案,这正是我需要的解决方案。非常感谢您的回复!
猜你喜欢
  • 2016-09-03
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 2012-12-12
  • 2015-04-02
相关资源
最近更新 更多