首页 > 解决方案 > Collectionview 重新加载功能不在 tableviewcell 内运行

问题描述

我在 tableviewcell 中创建了一个 collectionview。我将数据传递给 tableviewcell 的类。我试图调用 collectionview reload 函数,但该函数永远不会运行。

关于单元格:

TableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource 

    @IBOutlet weak var statCollection: UICollectionView!
    var collectionArray = [] // This creates the collection cells

    //DELEGATE SELF (datasource, delegate) at awakeFromNib()

    func loadItems(){
        print("load2")
        self.statelem = self.selectedStatparent?.statelements.sorted(byKeyPath: "statname", ascending: true)
        print("Elem: \(self.statelem?.count)") //=4
         DispatchQueue.main.async(execute: {
            self.statCollection.reloadData()
        })       
    }      

创建单元格,并调用函数(在 ViewController 上):

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = statTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! StatTableViewCell
    cell.statcategoryName.text = statkategoria?[indexPath.row].Categoryname
    cell.selectedStatparent = statkategoria?[indexPath.row]
    DispatchQueue.main.async(execute: {
        cell.loadItems()
    })
    return cell
}

标签: swiftuitableviewuicollectionview

解决方案


推荐阅读