首页 > 解决方案 > 为什么我的数据没有显示在我的 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
    }
    
    
    
    

}

标签: arraysswiftuitableviewfirebase-realtime-database

解决方案


推荐阅读