swift - 尝试从 Firebase 检索数据时未调用 tableView(_ tableView : UITableView, cellForRowAt indexPath: IndexPath) 函数

标签 swift uitableview firebase firebase-realtime-database

我是 Stack Overflow 的新手,所以请保持温柔!

我正在设计一个应用程序,它从 firebase 检索对象并在 firebase 中显示它们。

我在这里配置firebase:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FIRApp.configure()
    FIRDatabase.database().persistenceEnabled = true
    // Override point for customization after application launch.
    return true
}

但是,我发现当我运行该应用程序时,我只是得到一个空白的白屏。经过一番调试后,我意识到这是因为未到达 TableView (cellForRowAt indexPath: IndexPath) 代码块:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
let BUCSItem = items[indexPath.row]
   cell.textLabel?.text = BUCSItem.name
        print("should show bucs")
        return cell    
   }

我有其他所需的 tableView 方法:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

这是我的 viewDidLoad:

override func viewDidLoad() {   
    super.viewDidLoad()

    let ref = FIRDatabase.database().reference(withPath: "BUCS-items")
    tableView.allowsMultipleSelectionDuringEditing = false

    ref.queryOrdered(byChild: "BUCSgoals").observe(.value, with: { snapshot in
        var newItems: [BUCSItem] = []
        for child in snapshot.children {
            if let snapshot = child as? FIRDataSnapshot,
                let BUCSItem = BUCSItem(snapshot: snapshot) {
                newItems.append(BUCSItem)
            }
        }
        self.items = newItems

        self.tableView.reloadData()
    })

}

但这就是奇怪的部分。当我断开互联网连接时,它工作得很好,并且项目按预期显示在表格中。仅当连接到互联网时它们才不会显​​示。

这是断开连接时的样子: 连接后,其相同,但名称未出现在表中。

任何帮助将不胜感激!

最佳答案

我认为这是一个快速更新转换

ref.queryOrdered(byChild: "BUCSgoals").observe(.value

ref.queryOrdered(byChild: "BUCSgoals").observeSingleEvent(.value

关于swift - 尝试从 Firebase 检索数据时未调用 tableView(_ tableView : UITableView, cellForRowAt indexPath: IndexPath) 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51480178/

相关文章:

ios - VStack中的SwiftUI不同的对齐文本

ios - subview Controller 中的 UITableView 委托(delegate)

java - 如何使用 GeoFirestore (Java/Android) 在谷歌地图上显示多个标记?

javascript - VueJS : Wait firebase to load

swift - 给定一个 Swift `Any` 类型,我可以确定它是否是 `Optional` 吗?

ios - 重新加载标签栏外观

ios - 在 didSelectRowAtIndexPath 创建的 UILabel 在同一位置不断重复

ios - TableviewCells 未按预期插入

firebase - 如何在 Firestore 中存储有关集合的元数据?

ios - 有没有办法在后台让我的应用程序在 CMMotionActivity 更改时收到通知?