gpt4 book ai didi

swift - 从 Firestore 填充 UITableView

转载 作者:可可西里 更新时间:2023-11-01 00:55:28 26 4
gpt4 key购买 nike

这感觉非常基本,但我无法让它工作。从 FIrestore 中读取的内容不会显示在 tableview 中。据我所知,tableview 在 scheduleArray 数组从 FIrestore 填充之前呈现。如果我使 scheduleArray 文字一切正常。loadData() 函数中的 print 语句可以很好地打印数据库的内容。因此,与 Firestore 的连接按预期工作。

如何在呈现 TableView 之前进行数据库读取和数组填充?

class ListScheduleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var scheduleIDarray = [String]() // <-- If this is made an array literal, everything works
var db: Firestore!
override func viewDidLoad() {
super.viewDidLoad()

db = Firestore.firestore()
loadData()

}
func loadData() {
db.collection("schedules").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {

self.scheduleIDarray.append(document.documentID)
}
}
print(self.scheduleIDarray) // <-- This prints the content in db correctly
}
}
@IBOutlet weak var tableView: UITableView!

//Tableview setup
func numberOfSections(in tableView: UITableView) -> Int {

return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

print("Tableview setup \(scheduleIDarray.count)")
return scheduleIDarray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customScheduleCell", for: indexPath) as! customScheduleviewCell
let schedule = scheduleIDarray[indexPath.row]

cell.scheduleName.text = schedule
print("Array is populated \(scheduleIDarray)")
return cell
}

}

最佳答案

在您的 viewController 中创建一个 tableView 的导出:@IBOutlet weak var tableView: UITableView! 并在 viewDidLoad()

中写入给定行
override func viewDidLoad() {
super.viewDidLoad()

tableView.dataSource = self
tableView.delegate = self
}

当您成功接收到数据时重新加载 tableView。

关于swift - 从 Firestore 填充 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49766747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com