Как получить документы Firestore и отобразить их в UITableView - PullRequest
0 голосов
/ 02 апреля 2020

Снимок экрана проекта Firestore Я хочу отобразить поле "first_name" для каждого документа в коллекции (в UITableView. UITableView просто отображается пустым при каждом запуске приложения.

КОД:

импорт UIKit импорт Firebase

класс HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var filterButton: UIButton!

@IBOutlet weak var userTableView: UITableView!

var usersArray = [String]()
var db: Firestore!


override func viewDidLoad() {
    super.viewDidLoad()
    userTableView.delegate = self
    userTableView.dataSource = self
    // Do any additional setup after loading the view.
    db = Firestore.firestore()
    loadData()

}


func loadData() {
    db.collection("users").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {

                self.usersArray.append(document.documentID)
            }
        }
        print(self.usersArray)

        self.userTableView.reloadData()
    }
}


func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    print("Tableview setup \(usersArray.count)")
    return usersArray.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = userTableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UserCell
    let user = usersArray[indexPath.row]

    cell.fullNameLabel.text = user
    print("Array is populated \(usersArray)")

    return cell
}

}

1 Ответ

0 голосов
/ 02 апреля 2020

Не похоже, что вы зарегистрировали ячейку в tableView. Попробуйте использовать: tableview.register(UserCell,forCellReuseIdentifier:"Your reuseIdentifier string"). Попробуйте вставить это в viewDidLoad.

...