有人可以告诉我如何将Firestore中的数据附加到数组中吗?我想做的是使它在视图出现时将数据附加到下面的数组中,然后附加到tableView的正确标签中。当我打电话给print("(document.data())")数据显示它应该在底部面板中做的方式,我只是不知道如何制作它以便它附加进入我的阵列。非常感谢任何帮助,非常感谢!!
struct DetailsAdded {
var titleName: String
var detailsName: String
}
var array = [DetailsAdded]()
override func viewDidLoad() {
super.viewDidLoad()
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 {
print("\(document.data())")
}
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CreateWorkoutCell", for: indexPath) as! CreateWorkoutTableViewCell
cell.titleLabel.text = array[indexPath.row].titleName
cell.detailsLabel.text = array[indexPath.row].detailsName
return cell
}
答案 0 :(得分:1)
if let snapshot = querySnapshot {
for document in snapshot.documents {
let data = document.data()
let name = data["name"] as? String ?? ""
let details = data["details"] as? String ?? ""
let newDetails = DetailsAdded(titleName: name, detailsName: details)
array.append(newDetails)
}
self.tableView.reloadData()
}