【问题标题】:Display firebase document field to iOS tableview将 Firebase 文档字段显示到 iOS 表格视图
【发布时间】: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


【解决方案1】:

您必须检查用户:如果 user == nil 您必须先登录用户。

override func viewDidLoad() {
    super.viewDidLoad()
    let user = Auth.auth().currentUser
    if user == nil {
        login()
    } else {
        // processing
    }
}

【讨论】:

  • 感谢黄!这行得通,但是如何将 Firestore 文档中的信息展示到 Tableview 中?我现在只能将它们打印到控制台
【解决方案2】:

这是一个部分解决方案(现在它发布到给定的 UITextfield,所以只需要弄清楚如何发布到 tableView):

    Firestore.firestore().collection("habits").getDocuments(completion: { (snapshot, error) in

        snapshot!.documents.forEach({ (document) in
            let nametyname = document.data()["author"]
            let street = document.data()["friend"]

            self.myTextiFieldi.text = nametyname as! String
            print(nametyname)
            print(street)
        })
    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 2012-06-22
    相关资源
    最近更新 更多