新的 FUITableViewDataSource - 如何使用? Swift 3

New FUITableViewDataSource - how to use? Swift 3

刚刚更新到较新的 FirebaseUI Pod - 发生了一些变化,但最大的变化之一是 FUI Table 视图的工作方式。我让它在旧版本上运行良好,但在下面遇到这个问题 - 并且缺少 documentation/examples.

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

我不明白索引路径是从哪里调用的。我需要做一个单独的 NSIndexPath 来传递吗?我也不太明白它应该放在哪里 - 以前,它是 FirebaseTableViewDataSource,我会把它设置在我的 viewDidLoad 中,它会直接从那里创建单元格等。它几乎现在看起来好像需要住在我的 cellForRowAtIndexPath 中。有人对此有什么建议吗?

这个最新版本的 test 使用 tableView:bind: 方法(看起来像是他们制作的 UITableView class 扩展),我能够让它工作。

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

还要确保您正在观察您的查询是否有变化,否则 tableView 将不会被填充

self.query?.observe(.value, with: { snapshot in
})