2016-03-05 22 views
0

在我的應用程序中,用戶在HomeViewController中選擇A或B,然後在LocationViewController中,從tableview中選擇一個位置,最後,ResultViewController從其他用戶。當用戶點擊一個表格行時,如何將計數添加到Firebase數據

我被困在LocationViewController

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,斯威夫特和火力地堡註冊用戶選擇火力地堡,我好不容易纔走到這一步用this tutorial在AppCoda幫助。任何幫助表示讚賞:)

+0

火力地堡有[大文件(https://www.firebase.com/docs/ios/)了。在這種情況下,您可能會查找['transaction()'](https://www.firebase.com/docs/ios/guide/saving-data.html#section-transactions),但最好是遵循快速入門,然後遵循編程指南。 –

+0

謝謝弗蘭克!是的,我通過閱讀指南和使用交易():)解決了它。 – Thien

+0

很酷。隨意自我回答,以便其他人可以從看到您的工作解決方案中受益。否則,我會投票結束太寬泛。 –

回答

0

這裏是我的解決方案,沒有我想要註冊用戶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) 
} 
相關問題