从Firebase中检索数据到tableview

在一个视图中,我的用户在input字段中键入一些文本,将其保存为ticket此过程没有问题,并成功添加到我的数据库中 )。

我希望所有这些ticket价值在每个单元格包含tickettableView中呈现。 我一直在玩耍,尽可能去玩,但还不够。

  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 } } 

首先在numberOfRowsInSection方法中,你需要返回你的数组的数量,而不是一些静态值。

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

第二次在closures你的数组初始化后,你需要重新加载tableView

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