2017-12-31 66 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 
} 

를 오버라이드 (override) 그리고 가져 오는에 대한 귀하의 요구 사항 실제 편도로 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 
} 
관련 문제