我有一个collectionView
标头,其中有一个水平collectionView
[主collectionView
是垂直的。)
标头已渲染,但是我没有得到水平collectionView
的模型计数
我确保了以下内容;
collectionViews
'delegate
和dataSource
均设置为
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()
}
}
}
答案 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