我正在尝试使用NSCollectionView
和NSCollectionViewDiffableDataSource
创建具有动态数目的节的NSCollectionViewCompositionalLayout
。
收集视图用于显示搜索结果,部分的数量取决于找到的结果的类型和数量。每个部分使用不同的布局显示其内容类型。
数据源被声明为NSCollectionViewDiffableDataSource<String,SearchResult>
,其中SearchResult
是使用Hashable
实现UUID()
的类。结果为零的部分不是空的,而是在集合视图中不存在。
我遇到的困难之一是确定与布局sectionIndex
中的sectionProvider
对应的段标识符:
private func createLayout() -> NSCollectionViewLayout {
let sectionProvider = { (sectionIndex: Int,layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection in
// How can the section identifier corresponding to `sectionIndex` be determined?
// Create a `NSCollectionLayoutSection` appropriate for the section type
let section = NSCollectionLayoutSection(/* ... */)
return section
}
return NSCollectionViewCompositionalLayout(sectionProvider: sectionProvider)
}
由于收集视图的部分数目取决于搜索结果,因此0
部分中显示的项目类型可能会更改。
有关示例项目,请参见https://github.com/sbooth/DiffableTest。