arrays - 为什么我的数据没有显示在我的 TableView 中?
问题描述
我从 Firebase 检索的数据没有显示在我的表格视图中,但它显示在我的快照中。我错过了什么?
这是我的代码:
import UIKit
import Firebase
import FirebaseDatabase
class NewConversationViewController: UITableViewController {
var users = [User]()
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
tableView.delegate = self
tableView.dataSource = self
fetchUser()
tableView.isHidden = false
}
func fetchUser() {
Database.database().reference().child("users").observeSingleEvent(of: .value, with: { snapshot in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.setValuesForKeys(dictionary)
self.users.append(user)
}
print(snapshot)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}, withCancel: nil)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let user = users[indexPath.section]
cell.textLabel?.text = user.name
return cell
}
}
解决方案
推荐阅读
- haskell - 将混合(可能是 Applicative)类型的参数应用于函数的最佳方法
- javascript - 如何检测用于创建 slug 的输入语言,如果不是英语,则 slug 输入使用 javascript 保持为空
- apache - 使用apache和nestjs重定向到主页的问题
- javascript - 有没有办法从 mongodb 检索数据作为键值映射,其中键是字段的值(使用 Mongoose)
- laravel - Laravel - 如何使用 external_user_id 发送 OneSignal 通知
- bash - Bash 脚本无法从列表中下载源包
- android - 从 Fabric 迁移到 FirebaseCrashlytics 后测试失败
- java - 如何检查两个节点是否是java中二叉树中的表亲?
- ruby - 如何在创建 Google 活动后收到的电子邮件通知中删除 Google Meet?
- php - 字符串数组计数并合并为一