【问题标题】:Cant display data after using .getDocuments in Firestore and trying to place the document IDs into an array and displaying them using a tableview在 Firestore 中使用 .getDocuments 并尝试将文档 ID 放入数组并使用 tableview 显示它们后无法显示数据
【发布时间】:2020-08-14 05:23:10
【问题描述】:

我有课

class List {

var name: String

init(name: String) {

    self.name = name

}
}

用于制作数组。我声明了数组

var providersList = [List]()

然后使用函数

func downloadData() {

    db.collection("\(finalSelection.lowercased())_providers").getDocuments() { (querySnapshot, err) in
         if let err = err {
             print("Error getting documents: \(err)")
         } else {
             for document in querySnapshot!.documents {
                self.providersList.append(List(name: "\(document.documentID)"))


             }
         }
     }

 }

下载数据。数据下载正确,因为没有错误,我还使用print() 确认数据正确。但是,当我使用 tableview 的功能时

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MoreCell", for: indexPath) as! MoreInfoTableViewCell

    cell.moreInfoLabel.text = providersList[indexPath.row].name

    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return providersList.count
}

表格视图未填充。我知道这种方法有效,因为我在代码的另一个区域使用了类似的东西(不是下载数据而是填充表格视图)

编辑:我尝试删除类并正常附加到数组,而不使用类中的.name。但是,这也没有做任何事情

【问题讨论】:

    标签: ios swift firebase uitableview google-cloud-firestore


    【解决方案1】:

    好的,所以当我躺在床上准备睡觉时,我找到了解决方案(笑)。在函数downloadData() 中,我必须将 self.tableView.reloadData() 添加到数组之后。我会为遇到像我这样的类似问题的任何人提供此内容。

    func 下载数据() {

    db.collection("\(finalSelection.lowercased())_providers").getDocuments() { (querySnapshot, err) in
         if let err = err {
             print("Error getting documents: \(err)")
         } else {
             for document in querySnapshot!.documents {
                self.providersList.append(List(name: "\(document.documentID)"))
                self.tableView.reloadData()
    
    
             }
         }
     }
    

    }

    【讨论】:

    • 相当不错的解决方案,但您需要立即移动 self.tableView.reloadData() for 循环但仍在 else 内,否则它将被重复调用刷新导致闪烁的ui。先填充,然后刷新。
    猜你喜欢
    • 2013-04-24
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    相关资源
    最近更新 更多