Ios 当用户点击表格行时,如何向Firebase数据添加计数
在我的应用程序中,用户在HomeViewController中选择A或B,然后在LocationViewController中,他们从表视图中选择一个位置,最后,ResultViewController统计其他用户的投票 我无法在LocationViewController中将用户选择注册到FirebaseIos 当用户点击表格行时,如何向Firebase数据添加计数,ios,swift,firebase,Ios,Swift,Firebase,在我的应用程序中,用户在HomeViewController中选择A或B,然后在LocationViewController中,他们从表视图中选择一个位置,最后,ResultViewController统计其他用户的投票 我无法在LocationViewController中将用户选择注册到Firebase func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if userVote == "A" {
// add vote count by 1 to A of Location ABC in Firebase
// register this vote to user's path too
} else {
// add vote count by 1 to B of Location ABC in Firebase
// register this vote to user's path too
}
}
我是iOS的新手,Swift和Firebase,在AppCoda的帮助下我成功地走到了这一步。非常感谢您的帮助:)这是我的解决方案,其中没有我想注册用户ID投票的部分。不确定这是否是最有效的方法,请随意评论:
var businesses = [Business]()
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow!
let business = businesses[indexPath.row]
// Get the Firebase key of the selected row
let businessId = business.businessKey
// Get the path to the vote data
let voteARef = ref.childByAppendingPath(businessId).childByAppendingPath("voteA")
let voteBRef = ref.childByAppendingPath(businessId).childByAppendingPath("voteB")
if userVote == "voteA" {
voteARef.runTransactionBlock({
(currentData:FMutableData!) in
var value = currentData.value as? Int
if (value == nil) {
value = 0
}
currentData.value = value! + 1
return FTransactionResult.successWithValue(currentData)
})
} else {
voteBRef.runTransactionBlock({
(currentData:FMutableData!) in
var value = currentData.value as? Int
if (value == nil) {
value = 0
}
currentData.value = value! + 1
return FTransactionResult.successWithValue(currentData)
})
}
performSegueWithIdentifier("ResultSegue", sender: self)
}
Firebase也有。在这种情况下,您可能正在寻找,但最好只遵循快速入门和编程指南,然后从头到脚。谢谢Frank!是的,我通过阅读指南并使用transaction():)Cool解决了这个问题。自由地自我回答,这样其他人就可以从看到您的工作解决方案中获益。否则,我会投票表决,因为太宽了。