2017-12-31 278 views
0

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 
    } 
} 

答えて

0

あなたは間違った方法

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

をオーバーライドし、Interface Builderでセルスタイルを設計し、フェッチのために、要件の実際の方法を1としてcellForRowAt

let cell = tableView.dequeueReusableCell(withCellIdentifier: cellId, for: indexPath) 
0

でこのメソッドを使用していますテーブルビューセルのレコードは次のとおりです。

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

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(withCellIdentifier: cellId, for: indexPath) 
    let user = users[indexPath.row] 
    cell.detailTextLabel?.text = user.userId 
    return cell 
} 
関連する問題