gpt4 book ai didi

ios - 当用户点击表格行时如何向 Firebase 数据添加计数

转载 作者:行者123 更新时间:2023-11-30 13:38:44 25 4
gpt4 key购买 nike

在我的应用程序中,用户在 HomeViewController 中选择 A 或 B,然后在 LocationViewController 中从表视​​图中选择一个地点,最后,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 新手,在 this tutorial 的帮助下我成功做到了这一点在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)
}

关于ios - 当用户点击表格行时如何向 Firebase 数据添加计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35815528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com