FirebaseデータベースからすべてのユーザIDを取得しています。私がプログラムを実行すると、26行目のコードでコンソール上のすべてのユーザーIDのスナップショットを見ることができます。しかし、コードは表のセルに書き込んでいません。私はチュートリアルでこれを行いました。ビデオとすべて同じです。しかし、それは私のためには動作しませんどこに問題がありますか?Swift 3 FirebaseデータをTableViewに書き込む
class ChatInfo: UITableViewController {
let cellId = "cellId"
var users = [User]()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Geri", style: .plain, target:self, action: #selector(handleCancel))
fetchUser()
}
func handleCancel() {
dismiss(animated: true, completion: nil)
}
func fetchUser() {
Database.database().reference().child("locations").observe(.childAdded, with: {(snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.userId = dictionary["userId"] as! String
print(user.userId) // IT PRINTS ALL USERS TO CONSOLE
self.users.append(user)
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
} , withCancel: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
return users.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
let user = users[indexPath.row]
cell.detailTextLabel?.text = user.userId
return cell
}
}