【发布时间】:2017-12-17 16:11:56
【问题描述】:
我正在使用 Swift 和 Firebase 实施问答程序。我希望用户能够喜欢问题的答案。我处理喜欢的数据库结构是:
answerLikes
answerID
userID : true
answers
...
posts
...
users
...
我试图根据这个数据结构来实现我的程序。你可以在我的 TableViewController 中看到代码:
@IBAction func likeButtonClicked(_ sender: UIButton) {
if let indexPath = self.tableView.indexPathForSelectedRow {
ref = Database.database().reference()
print(indexPath.row)
ref.child("answerLikes").child(answers[indexPath.row].id).observeSingleEvent(of: .value, with: {
(snapshot) in
let value = snapshot.value as? NSDictionary
if value?[Auth.auth().currentUser?.uid] == nil {
sender.setImage(UIImage(named: "filledHeart.png"), for: .normal)
self.ref.child("answerLikes").child(self.answers[indexPath.row].id).updateChildValues([(Auth.auth().currentUser?.uid)! : true])
} else {
sender.setImage(UIImage(named: "emptyHeart.png"), for: .normal)
self.ref.child("answerLikes").child(self.answers[indexPath.row].id).removeValue()
}
})
}
}
我的问题是,在这个函数定义中,我不知道“点击的按钮在哪个单元格中?”。我们在表格视图函数中使用 indexPath 来处理这个问题。所以我也尝试在这段代码中使用它,但是,我的代码只有在用户单击单元格然后单击like按钮时才有效。
谁能帮我解决这个问题?我真的对这个“赞帖子”功能有严重的问题。谢谢。
【问题讨论】:
标签: ios swift firebase firebase-realtime-database