New FUITableViewDataSource - how to use? Swift 3

假如想象 提交于 2019-12-06 03:25:08

问题


Just updated to the newer FirebaseUI Pod - a few things have changed but one of the big ones is the way that the FUI Table View works. I had it working well on an older version but am struggling with this below - and the lack of documentation/examples.

 self.dataSource = FUITableViewDataSource(query: <#T##FIRDatabaseQuery#>, view: <#T##UITableView#>, populateCell: <#T##(UITableView, IndexPath, FIRDataSnapshot) -> UITableViewCell#>)

I don't understand where the indexpath is being called from. Do I need to make a seperate NSIndexPath to pass into that? I also don't really understand where this is supposed to live - previously, with it was FirebaseTableViewDataSource, I would set it in my viewDidLoad, and it would create the cells etc straight from that. It almost now looks as though it needs to live in my cellForRowAtIndexPath. Does anyone have any advice on this?


回答1:


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


来源:https://stackoverflow.com/questions/40855215/new-fuitableviewdatasource-how-to-use-swift-3

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