【发布时间】:2019-11-13 08:24:20
【问题描述】:
我有一个 collectionView 标题,里面有一个水平的 collectionView [主要的 collectionView 是垂直的]。
标题被渲染出来,但是,我没有得到水平collectionView的模型计数
我已确保以下内容;
-
collectionViews'delegate和dataSource都设置为self - 正在检索数据
- 我在
.main队列的两个collectionViews 上调用.reloadData()。
标题代码
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()
}
}
}
【问题讨论】:
标签: ios swift uicollectionview uicollectionreusableview