Swift Firebase就像表视图中的帖子一样

问题描述 投票:1回答:1

我正在使用Firebase在Swift中实施问答程序。我希望在tableViewCell中有一个类似的按钮。但是,我遇到了问题,因为post的数据在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)
        }
    })
}
ios swift firebase firebase-realtime-database firebase-authentication
1个回答
0
投票

更新

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

您可以在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代码的Controller中确认它。

2.当单击按钮时,激活一个委托,您将在mainViewController中获得回调。

实现这一目标的第二种方式

1.在AnswerCell类中点击类似按钮的IBOutlet。

2.不要做IBAction。

你的代码看起来像这样

  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)
    }
})
   }
© www.soinside.com 2019 - 2024. All rights reserved.