【发布时间】:2020-04-02 01:59:22
【问题描述】:
Screenshot of Firestore Project 我想为集合中的每个文档显示“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
}
}
【问题讨论】:
标签: ios swift firebase google-cloud-firestore xcode11