在我的应用程序中,用户在HomeViewController中选择A或B,然后在LocationViewController中,他们从tableview中选择一个位置,最后,ResultViewController统计来自其他用户的投票。
我坚持在LocationViewController中将用户选择注册到Firebase
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的this tutorial的帮助下成功实现了这一目标。任何帮助表示赞赏:)
答案 0 :(得分:0)
这是我的解决方案,没有我想要注册投票给用户身份的部分。不确定它是否是最有效的方式,请随意发表评论:
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)
}