我正在使用(有点)新的 diffable 数据源和可组合的 collectionview 布局构建一个带有集合视图的视图控制器。我制作了自己的 Section 和 Row 枚举,可以对模型中的更改(一组照片)做出反应,这会触发applySnapshot
并且一切正常。
但是..在我的createLayout
函数中我只有一个部分索引可以使用??这对我来说似乎很奇怪,为什么这两种技术不能更好地协同工作?一个与SectionIdentifierType
and一起工作ItemIdentifierType
(太棒了!),另一个仍然是基于索引的?啊。
我想要做的是这样的事情:该Section.header
部分中的所有单元格都需要全屏宽度,并具有自动高度。该部分中的每个单元格Section.photos
都将获得固定大小。并且也可以有其他部分具有不同的单元格大小,并且某些部分可以是可选的。所以基于索引的系统真的很垃圾,不能硬编码第 0 部分是标题,第 1 部分是照片:顺序可以改变,事情可能是可选的,等等。
有什么更好的方法来处理这个问题?我当然可以[Section]
在视图控制器上存储一个数组,使用节索引来获取这种Section
情况,但是没有内置的方法来获取索引的节标识符吗?因为这两种技术是同时发布的,所以这似乎是一件合乎逻辑的事情。
class PhotosViewController: UIViewController {
enum Section: Equatable, Hashable {
case header
case photos
}
enum Row: Equatable, Hashable {
case header
case photo(Photo)
}
private lazy var dataSource = makeDataSource()
var photos: AnyPublisher<[Photo], Never>!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.collectionViewLayout = createLayout()
photos
.sink(receiveValue: applySnapshot)
.store(in: &subscriptions)
}
private func makeDataSource() -> UICollectionViewDiffableDataSource<Section, Row> {
return UICollectionViewDiffableDataSource(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
switch item {
case .header:
let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: ProfileHeaderCollectionViewCell.self)
return cell
case .photo(let photo):
let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: PhotoCollectionViewCell.self)
cell.configure(photo: photo)
return cell
}
}
}
private func applySnapshot(photos: [Photo]) {
var snapshot = NSDiffableDataSourceSnapshot<Section, Row>()
let headerSection = Section.header
snapshot.appendSections([headerSection])
snapshot.appendItems([.header], toSection: headerSection)
let items = photos.map { Row.photo($0) }
let photosSection = Section.photos
snapshot.appendSections([photosSection])
snapshot.appendItems(items, toSection: photosSection)
dataSource.apply(snapshot, animatingDifferences: false)
}
private func createLayout() -> UICollectionViewLayout {
let layout = UICollectionViewCompositionalLayout { [unowned self] sectionIndex, _ in
// I have to work with a sectionIndex instead of a Section case?
}
return layout
}
}