简体   繁体   English

从 Firebase 检索数据到 tableview

[英]Retrieving data from Firebase to tableview

In a view, my user types some text in a input field, which I save as a ticket ( this process works without problems and are added to my database successfully ).在视图中,我的用户在输入字段中键入了一些文本,我将其另存为ticket此过程没有问题并成功添加到我的数据库中)。

I want all these ticket values to be presented in a tableView in which each cell contains a ticket .我希望所有这些ticket值都显示在tableView中,其中每个单元格都包含一个ticket I've been playing around, and this is as far as I could go, but it is still not enough.我一直在玩,这是我所能做的,但仍然不够。

 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
}    

}

First of all in numberOfRowsInSection method you need to return your array's count instead of some static value.首先,在numberOfRowsInSection方法中,您需要返回数组的计数而不是一些静态值。

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

Second after initializng your array in closure you need to reload the tableView .其次,在闭包中初始化数组后,您需要重新加载tableView

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM