Firestore - 加载视图时附加到 tableView [英] Firestore - Append to tableView when view is loaded

查看:25
本文介绍了Firestore - 加载视图时附加到 tableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何将 Firestore 中的数据附加到数组中吗?我想要做的是让它在视图出现时将数据附加到下面的数组中,然后附加到 tableView 的正确标签中.当我调用 print("(document.data())") 时,数据显示了它在底部面板中应该做的方式,我只是不知道如何使它附加到我的数组中.任何帮助将不胜感激,非常感谢!!

Can someone please show me how I can append data from Firestore into an array? What I would like to do is make it so that when the view appears the data is appended into the below array and then into the correct labels of the tableView. When I call print("(document.data())") the data shows the way it should do in the bottom panel, I just don't know how to make it so that it appends into my array. Any help would be greatly appreciated, Many thanks!!

Firestore

Xcode 底部面板

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
}

推荐答案

    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()
    }

这篇关于Firestore - 加载视图时附加到 tableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆