特定CollectionViewCell的TableView数据

问题描述 投票:0回答:1

我无法加载与收集单元格行相关的数据。为了进行测试,我打印了该程序的流程,即使在CollectionView的cellForItemAt内部调用tableView.reload,它似乎也可以处理所有的CollectionView单元。任何帮助将不胜感激。

打印声明:索引路径行CollectionView 0索引路径行CollectionView 1表视图索引路径行TableView 0表视图索引路径行TableView 0

我在Firebase中有此收藏集:日历index: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)

                }
            }
    }
}
ios swift uicollectionview tableview
1个回答
0
投票

Firebase在后台线程中返回。在完成获取并填充了UITableView之后,您不会重新加载UICollectionViewdataSource。您需要在主线程上添加一个重载方法。像这样的东西:

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

DispatchQueue.main.async {
   //reload here
}
© www.soinside.com 2019 - 2024. All rights reserved.