【发布时间】:2020-06-04 20:33:13
【问题描述】:
我无法加载与集合单元格行相关的数据。 出于测试目的,我打印了程序的流程,它似乎在进入 tableView 之前执行了所有 CollectionView 单元格,即使我在 CollectionView 的 cellForItemAt 内调用 tableView.reload 也是如此。 任何帮助将不胜感激。
打印声明: 索引路径行 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)
}
}
}
}
【问题讨论】:
-
你在哪里打电话
populateDataSource? -
我在 viewDidLoad 和 CollectionView cellforItem 上试过如下: cell?.tableView.delegate = self cell?.tableView.dataSource = self print("index path row CollectionView (indexPath.row)") / / populateDataSource(index: indexPath.row) // DispatchQueue.main.async { cell?.tableView.reloadData() // 让 indexPath = IndexPath(row: self.calEvent.count - 1 , section:0) // 单元格? .tableView.scrollToRow(at: indexPath, at: .top, animated: false) // } 返回单元格! }
标签: ios swift uicollectionview tableview