2016-09-29 37 views
0

在視圖中,我的用戶在輸入字段中鍵入一些文本,我將其保存爲ticket此過程沒有問題,併成功添加到我的數據庫)。從Firebase檢索數據到表格視圖

我希望所有這些ticket值在tableView中呈現,其中每個單元格包含ticket。我一直在玩耍,儘可能地去玩,但這還不夠。所有numberOfRowsInSection方法

import UIKit 
import Firebase 

class TestTableViewController: UITableViewController { 

let cellNavn = "cellNavn" 

    var holes: [FIRDataSnapshot]! = [] 


let CoursesRef = FIRDatabase.database().reference().child("Ticketcontainer") 


override func viewDidLoad() { 
    super.viewDidLoad() 

    CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshot in 
     self.holes = snapshot.childSnapshotForPath("tickets").children.allObjects as! [FIRDataSnapshot] 
    }) 

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(handleCancel)) 

    tableView.registerClass(UserCell.self, forCellReuseIdentifier: cellNavn) 


} 


func handleCancel() { 
    dismissViewControllerAnimated(true, completion: nil) 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 2 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellNavn, forIndexPath: indexPath) as! UserCell      
    cell.textLabel?.text = self.holes[indexPath.row].value as? String 

    return cell 

} 

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 72 
}  

} 

回答

0

首先,你需要,而不是返回一些靜態值的您的數組的計數。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return holes.count 
} 

第二次在關閉數組後初始化您需要重新加載tableView

CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshot in 
    self.holes = snapshot.childSnapshotForPath("tickets").children.allObjects as! [FIRDataSnapshot] 
    self.tableView.reloadData() 
}) 
+0

我得到它的工作與你的幫助,但我也出於某種原因不得不改變.ChildAdded .Value。 我現在得到整個表格,但是我仍然會用.Childadded思考它會顯示已應用於票據列表的最後一張「票據」? – Frederic

+0

@Frederic不明白你說的,請詳細解釋你面臨的問題。 –

+0

對不起!起初,當我實現你的代碼時,我只是得到了一個空白的白色表格視圖,然後我將我的observeEventType中的「.ChildAdded」更改爲「.Value」 然後它確實奏效了。它現在顯示了我數據庫中的所有值。 現在我只是想知道是否有可能只顯示「剛剛由用戶鍵入」的票證,(這就是爲什麼我試着用.Childadded,但它只是空白。 希望我讓自己更清楚,我感謝你的幫助! – Frederic

相關問題