【问题标题】:Retrieving data from Firebase to tableview将数据从 Firebase 检索到 tableview
【发布时间】:2016-09-29 16:12:03
【问题描述】:

在视图中,我的用户在输入字段中键入了一些文本,我将其另存为 ticket此过程正常运行并成功添加到我的数据库中)。

我希望所有这些ticket 值都显示在tableView 中,其中每个单元格都包含一个ticket。我一直在玩,这是我能做到的,但仍然不够。

 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
}    

}

【问题讨论】:

    标签: ios swift uitableview firebase firebase-realtime-database


    【解决方案1】:

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

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

    【讨论】:

    • 在您的帮助下我得到了它,但我也出于某种原因不得不将 .ChildAdded 更改为 .Value。我现在得到了整张桌子,但是我仍然认为 .Childadded 它会显示已应用于票证列表的最后一张“票证”?
    • @Frederic 不明白你说什么,请详细解释你遇到的问题。
    • 对不起!起初,当我实现您的代码时,我只是得到了一个空白的白色表格视图,然后我将 observeEventType 中的“.ChildAdded”更改为“.Value”,然后它确实起作用了。它现在显示了我数据库中的所有值。现在我只是想知道是否可以只显示“刚刚由用户输入”的票,(这就是我尝试使用 .Childadd 的原因,但它只是空白。希望我让自己更清楚,我感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 2018-11-07
    • 2020-08-17
    相关资源
    最近更新 更多