我是 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/