New FUITableViewDataSource - how to use? Swift 3

笑着哭i 提交于 2019-12-04 07:06:00
Keith Kurak

The test for this latest version uses a tableView:bind: method (seems like a UITableView class extension they made) and I was able to get it to work.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let firebaseRef = FIRDatabase.database().reference().child(/*insert path for list here*/)

    let query = firebaseRef.queryOrderedByKey() /*or a more sophisticated query of your choice*/

    let dataSource = self.tableView.bind(to: query, populateCell: { (tableView: UITableView, indexPath: IndexPath, snapshot: FIRDataSnapshot) -> UITableViewCell in

        let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)

        let value = snapshot.value as! NSDictionary

        let someProp = value["someProp"] as? String ?? ""

        cell.textLabel?.text = someProp

        return cell
    })
}

Also make sure you are observing your query for changes or else the tableView won't get populated

self.query?.observe(.value, with: { snapshot in
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!