【问题标题】:UITableView not loading Firebase Snapshot Data.UITableView 未加载 Firebase 快照数据。
【发布时间】:2017-04-10 12:20:26
【问题描述】:

我有一个具有以下结构的 Firebase 数据库: “用户” -uid - 姓名 - 电子邮件 .我想将“用户”电子邮件和名称输入到 XCode 中的 UITableviewController 表视图中。数据可以在我的控制台中看到,但没有附加到我的表格视图中

class DictionaryTableViewController: UITableViewController {

var ref: FIRDatabaseReference!
let cellID = "Cell"
var refHandle: UInt!
var userList = [Users]()

override func viewDidLoad() {
  super.viewDidLoad()
  //Set firebase database reference
  ref = FIRDatabase.database().reference()

  //Retrieve posts and listen for changes
  refHandle = ref?.child("users").observe(.childAdded, with: { (snapshot) in
     //Code that executes when child is added
     if let dict = snapshot.value as? [String: AnyObject] {
        let user = Users()
        user.name = snapshot.childSnapshot(forPath: "name").value as? String
        print(user.name)
        user.email = snapshot.childSnapshot(forPath: "email").value as? String
        print(user.email)
        print("databaseHandle was called")
        for user in self.userList {
           print(user)
           self.userList.append(user)

        }
        self.tableView.reloadData()
     }
  })
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return userList.count
  }
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellID)
     cell.textLabel?.text = userList[indexPath.row].name.self
     cell.textLabel?.text = userList[indexPath.row].email.self
     return cell

   }
 }
}

【问题讨论】:

  • 在主线程上重新加载表。
  • 为什么会得到自己的姓名和邮箱? userList[indexPath.row].name.self

标签: ios swift uitableview firebase


【解决方案1】:

删除这个:

self.tableView.reloadData()

在 if let 语句之后添加:

DispatchQueue.main.async{
    self.tableView.reloadData()
}

【讨论】:

  • 我已将此添加到我的代码中,但它仍然没有在 tableview 上显示任何内容。请参阅下面的代码。
【解决方案2】:

像这样;仍然没有显示表格上的数据。

//Retrieve posts and listen for changes func fetchUserData(with completion:@escaping (Bool)->()) { refHandle = ref?.child("users").observe(.childAdded, with: { (snapshot) in //Code that executes when child is added if (snapshot.value as? [String: AnyObject]) != nil { let user = Users() user.name = snapshot.childSnapshot(forPath: "name").value as? String print(user.name) DispatchQueue.main.async{ user.email = snapshot.childSnapshot(forPath: "email").value as? String print(user.email) print("databaseHandle was called") for user in self.userList { print(user) self.userList.append(user) self.userTable.reloadData() }

【讨论】:

    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2023-03-26
    相关资源
    最近更新 更多