【问题标题】:TableView data for a Specific CollectionViewCell特定 CollectionViewCell 的 TableView 数据
【发布时间】: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


【解决方案1】:

Firebase 在后台线程中返回。在提取完成并且您的 dataSource 已填充后,您不会重新加载 UITableViewUICollectionView。您需要在主线程上添加一个 reload 方法。像这样的:

for doc in (snap.documents) { ... }

DispatchQueue.main.async {
   //reload here
}

【讨论】:

  • 我正在调用 CollectionView cellforItem,我需要重新加载 collectionView 吗? 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 populateDataSource(index: indexPath.row) DispatchQueue.main.async { cell?.tableView.reloadData() let indexPath = IndexPath(row: self.calEvent. count - 1)} 返回单元格!}
  • 您应该只调用viewDidLoad 中的population 方法,然后像我上面发布的那样在获取完成时重新加载。在cellForItem 中调用它只会导致该方法在每次滚动时重新获取数据,这是不必要的昂贵。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 2020-11-04
相关资源
最近更新 更多