[英]TableView data for a Specific CollectionViewCell
我無法加載與集合單元格行相關的數據。 出於測試目的,我打印了程序的流程,它似乎在進入 tableView 之前執行了所有 CollectionView 單元格,即使我在 CollectionView 的 cellForItemAt 內調用 tableView.reload 也是如此。 任何幫助將不勝感激。
PRINT STATEMENTS:索引路徑行 CollectionView 0 索引路徑行 CollectionView 1 表視圖索引路徑行 TableView 0 表視圖索引路徑行 TableView 0
我在 Firebase 中有這個集合:日歷索引:0(這是 CollectionCell indexPath.row)標題:“示例 1 索引 0”索引:0 標題:“示例 2 索引 0”索引:1 標題:“測試 1 索引 1”
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? CollectionViewCell
cell?.tableView.delegate = self
cell?.tableView.dataSource = self
print("index path row CollectionView \(indexPath.row)")
cell?.tableView.reloadData()
return cell!
}
extension ViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if calEvent.count == 0 {
return 1
} else {
return calEvent.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableviewCell",
for: indexPath) as? TableViewCell
print("table view index path row TableView \(indexPath.row)")
if indexPath.row == calEvent.count {
cell?.label.text = "no event"
} else {
cell?.label.text = calEvent[indexPath.row].title
}
return cell!
}
func tableView(_: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func populateDataSource(index: Int) {
self.db.collection("calendar")
.whereField("index", isEqualTo: index)
.addSnapshotListener { (snapshot, error ) in
if let e = error {
print("error \(e)")
} else {
self.calEvent.removeAll()
guard let snap = snapshot else { return }
for doc in (snap.documents) {
let data = doc.data()
print(doc.data())
let title = data["title"] as? String ?? ""
guard let stamp = data["date"] as? Timestamp else {
return
}
let dateStamp = stamp.dateValue()
let newEvent = CalEvent(category: "", title: title, date: dateStamp, paciente: "")
self.calEvent.append(newEvent)
}
}
}
}
Firebase 在后台線程中返回。 在獲取完成並填充dataSource
源后,您不會重新加載UITableView
或UICollectionView
。 您需要在主線程上添加一個 reload 方法。 像這樣的東西:
for doc in (snap.documents) { ... }
DispatchQueue.main.async {
//reload here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.