【问题标题】:New FUITableViewDataSource - how to use? Swift 3新的 FUITableViewDataSource - 如何使用?斯威夫特 3
【发布时间】:2017-04-12 19:40:04
【问题描述】:

刚刚更新到较新的 FirebaseUI Pod - 发生了一些变化,但其中最重要的一点是 FUI 表视图的工作方式。我让它在旧版本上运行良好,但在下面遇到了这个问题 - 并且缺乏文档/示例。

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

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

【问题讨论】:

    标签: swift firebase firebase-realtime-database firebaseui


    【解决方案1】:

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

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

    【讨论】:

    • 它不适合我,你能指出我的工作样本吗
    猜你喜欢
    • 2017-03-24
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 2017-07-22
    • 2014-08-31
    • 1970-01-01
    相关资源
    最近更新 更多