UICollectionReusableView内部的UICollectionViewCells是否未呈现?

时间:2019-11-13 08:24:20

标签: ios swift uicollectionview uicollectionreusableview

我有一个collectionView标头,其中有一个水平collectionView [主collectionView是垂直的。)

标头已渲染,但是我没有得到水平collectionView的模型计数

我确保了以下内容;

  • collectionViews'delegatedataSource均设置为 self
  • 正在检索数据
  • 我正在.reloadData()队列的两个collectionView上调用.main

标题代码

private var collectionViewHeader: ExploreTableHeaderView = {
        let view = ExploreTableHeaderView()
        return view
    }()

class ExploreTableHeaderView: UICollectionReusableView {
    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = 16
        layout.minimumLineSpacing = 0
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.backgroundColor = .orange
        view.contentInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        view.alwaysBounceHorizontal = true
        view.showsHorizontalScrollIndicator = false
        view.allowsSelection = true
        view.translatesAutoresizingMaskIntoConstraints = false
        view.register(ExploreCategoryCollectionViewCell.self, forCellWithReuseIdentifier: Identifier.categoryCell)
        return view
    }()

// Override frame, addSubView etc...

标头实现

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    switch kind {
    case UICollectionView.elementKindSectionHeader:
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Identifier.headerCell, for: indexPath) as! ExploreTableHeaderView
        return headerView
    default:
    return UICollectionReusableView()
    }
}

数据源

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == collectionViewHeader.collectionView {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifier.categoryCell, for: indexPath) as? ExploreCategoryCollectionViewCell {
            let category = self.categories[indexPath.item]
            cell.category = category
            return cell
        }
    } else {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifier.itemCell, for: indexPath) as? ExploreItemCollectionViewCell {
            let item = self.recentItems[indexPath.item]
            cell.item = item
            return cell
        }
    }
    return UICollectionViewCell()
}

刷新视图

private var categories = [Category]() {
        didSet {
            DispatchQueue.main.async {
                self.collectionViewHeader.collectionView.reloadData()
            }
        }
    }

    private var recentItems = [Item]() {
        didSet {
            DispatchQueue.main.async {
                self.itemsCollectionView.reloadData()
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您能否详细说明如何设置委托和数据源?

您说两个collectionViews委托和dataSource都设置为self,但是我没有看到完整的代码,所以我的假设是您引用的self对于每个collectionViews都是不同的

我的假设是您需要在此代码行中设置委托和数据源

private var collectionViewHeader: ExploreTableHeaderView?

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Identifier.headerCell, for: indexPath) as! ExploreTableHeaderView
        headerView.collectionView.dataSource = self
        headerView.collectionView.delegate = self
        collectionViewHeader = headerView
            return headerView
        default:
        return UICollectionReusableView()
        }
    }

编辑: 这里的示例代码 https://github.com/aiwiguna/CollectionViewReuse