【发布时间】:2019-02-24 01:40:07
【问题描述】:
如何在 Swift 上将 Firestore 集合中的数据显示到 tableView?
此代码运行(见下文),但我想直接从 Firestore 数据库导入,而不是硬类型化数据:
var habits = [Habit(id: "1", author: "Maiuran", text: "heyhey")]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "habitCell", for: indexPath) as! HabitTableViewCell
cell.set(habit: habits[indexPath.row])
return cell
}
下面的代码可以打印到控制台,但不确定如何显示
let name = Auth.auth().currentUser?.uid
Firestore.firestore().collection("habits").whereField("author", isEqualTo: name).getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
}
}
}
这是数据库:Firestore Database structure
非常感谢您的宝贵时间和建议!
【问题讨论】:
-
您似乎还没有尝试实际处理 Firestore。您是否阅读过从 Firestore 中读取数据的文档? firebase.google.com/docs/firestore/query-data/get-data
-
我尝试了以下@DougStevenson:
-
let name = Auth.auth().currentUser?.uid Firestore.firestore().collection("habits").whereField("author", isEqualTo: name).getDocuments() { ( querySnapshot, err) in if let err = err { print("Error getting documents: (err)") } else { for document in querySnapshot!.documents { print("(document.documentID) => (document.data() )") } } }
-
这工作并打印到控制台,但坚持在实际应用程序上显示它:/
标签: swift google-cloud-firestore tableview