New FUITableViewDataSource - how to use? Swift 3

2020-07-11 09:55发布

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条回答
闹够了就滚
2楼-- · 2020-07-11 10:36

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
})
查看更多
登录 后发表回答