Ios 从未对UICollectionViewDiffableDataSource执行CellProvider闭包 class PropertyCollViewController:UIViewController{ @ibvar collectionView:UICollectionView! var sections=Person.getSectionData() typealias PropertyDataSource=UICollectionViewDiffableDataSource typealias PropertySnapshot=NSDiffableDataSourceSnapshot 私有变量数据源:PropertyDataSource! 重写func viewDidLoad(){ super.viewDidLoad() self.title=“集合” collectionView.collectionViewLayout=configureLayout() configureDataSource() } func configureLayout()->UICollectionViewCompositionLayout{ let size=NSCollectionLayoutSize( 宽度尺寸:。分数宽度(1.0), 高度尺寸:。绝对值(44) ) let item=NSCollectionLayoutItem(layoutSize:size) 设groupSize=NSCollectionLayoutSize(宽度维度:。分数宽度(0.5),高度维度:。绝对(44)) let group=NSCollectionLayoutGroup.horizontal(layoutSize:groupSize,子项:[项]) let section=NSCollectionLayoutSection(组:组) section.contentInsets=NSDirectionalEdgeInsets(顶部:10,前导:10,底部:10,尾随:10) section.interGroupSpacing=0 返回UICollectionViewCompositionLayout(截面:截面) } func configureDataSource(){ dataSource=UICollectionViewDiffableDataSource(collectionView:collectionView,cellProvider:{(collectionView,indexpath,property)->UICollectionViewCell?在 打印(“酷”) 让cell=collectionView.dequeueReusableCell(带有ReuseIdentifier:“PropertyViewCell”,for:indexpath)作为?PropertyViewCell 单元格?.titleLabel.text=property.asset.name 单元格?.backgroundColor=.red 返回单元 }) dataSource.supplementaryViewProvider={(collectionView,kind,indexpath)在 guard kind==UICollectionView.elementKindSectionHeader else{ 归零 } guard let view=collectionView.dequeueReusableSupplementaryView(ofKind:kind,带有ReuseIdentifier:“HeaderView”,for:indexpath)作为?HeaderView-else{ 法塔莱罗() } 让section=self.dataSource.snapshot().sectionIdentifiers[indexpath.section] view.sectionTitle.text=section.id.rawValue view.backgroundColor=.red 返回视图 } var snapShot=PropertySnapshot() 快照.附加节(节) dataSource.apply(快照、动画差异:true) } }
这是因为您没有将项目附加到任何位置,因此快照为空。附加节时,仅将节作为标识符附加。您没有附加该节及其所有项Ios 从未对UICollectionViewDiffableDataSource执行CellProvider闭包 class PropertyCollViewController:UIViewController{ @ibvar collectionView:UICollectionView! var sections=Person.getSectionData() typealias PropertyDataSource=UICollectionViewDiffableDataSource typealias PropertySnapshot=NSDiffableDataSourceSnapshot 私有变量数据源:PropertyDataSource! 重写func viewDidLoad(){ super.viewDidLoad() self.title=“集合” collectionView.collectionViewLayout=configureLayout() configureDataSource() } func configureLayout()->UICollectionViewCompositionLayout{ let size=NSCollectionLayoutSize( 宽度尺寸:。分数宽度(1.0), 高度尺寸:。绝对值(44) ) let item=NSCollectionLayoutItem(layoutSize:size) 设groupSize=NSCollectionLayoutSize(宽度维度:。分数宽度(0.5),高度维度:。绝对(44)) let group=NSCollectionLayoutGroup.horizontal(layoutSize:groupSize,子项:[项]) let section=NSCollectionLayoutSection(组:组) section.contentInsets=NSDirectionalEdgeInsets(顶部:10,前导:10,底部:10,尾随:10) section.interGroupSpacing=0 返回UICollectionViewCompositionLayout(截面:截面) } func configureDataSource(){ dataSource=UICollectionViewDiffableDataSource(collectionView:collectionView,cellProvider:{(collectionView,indexpath,property)->UICollectionViewCell?在 打印(“酷”) 让cell=collectionView.dequeueReusableCell(带有ReuseIdentifier:“PropertyViewCell”,for:indexpath)作为?PropertyViewCell 单元格?.titleLabel.text=property.asset.name 单元格?.backgroundColor=.red 返回单元 }) dataSource.supplementaryViewProvider={(collectionView,kind,indexpath)在 guard kind==UICollectionView.elementKindSectionHeader else{ 归零 } guard let view=collectionView.dequeueReusableSupplementaryView(ofKind:kind,带有ReuseIdentifier:“HeaderView”,for:indexpath)作为?HeaderView-else{ 法塔莱罗() } 让section=self.dataSource.snapshot().sectionIdentifiers[indexpath.section] view.sectionTitle.text=section.id.rawValue view.backgroundColor=.red 返回视图 } var snapShot=PropertySnapshot() 快照.附加节(节) dataSource.apply(快照、动画差异:true) } },ios,swift,diffabledatasource,nsdiffabledatasourcesnapshot,Ios,Swift,Diffabledatasource,Nsdiffabledatasourcesnapshot,这是因为您没有将项目附加到任何位置,因此快照为空。附加节时,仅将节作为标识符附加。您没有附加该节及其所有项 请参阅文档。就是这样。我自己想出来的。过了一会儿。然而,你能接受这个答案吗?出于某种原因,人们对正确答案投了反对票。我投了赞成票。不知道为什么会有反对票。此外,我的支持票似乎并不重要,因为我的声誉很低。很抱歉。 class PropertyCollViewController: UIViewController { @IBOutlet weak var collectionVie
请参阅文档。就是这样。我自己想出来的。过了一会儿。然而,你能接受这个答案吗?出于某种原因,人们对正确答案投了反对票。我投了赞成票。不知道为什么会有反对票。此外,我的支持票似乎并不重要,因为我的声誉很低。很抱歉。
class PropertyCollViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
var sections = Person.getSectionData()
typealias PropertyDataSource = UICollectionViewDiffableDataSource<Person.Section, Property>
typealias PropertySnapshot = NSDiffableDataSourceSnapshot<Person.Section, Property>
private var dataSource: PropertyDataSource!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Collections"
collectionView.collectionViewLayout = configureLayout()
configureDataSource()
}
func configureLayout() -> UICollectionViewCompositionalLayout {
let size = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(44)
)
let item = NSCollectionLayoutItem(layoutSize: size)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .absolute(44))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)
section.interGroupSpacing = 0
return UICollectionViewCompositionalLayout(section: section)
}
func configureDataSource() {
dataSource = UICollectionViewDiffableDataSource<Person.Section, Property>(collectionView: collectionView, cellProvider: { (collectionView, indexpath, property) -> UICollectionViewCell? in
print("Cool")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PropertyViewCell", for: indexpath) as? PropertyViewCell
cell?.titleLabel.text = property.asset.name
cell?.backgroundColor = .red
return cell
})
dataSource.supplementaryViewProvider = { (collectionView, kind, indexpath) in
guard kind == UICollectionView.elementKindSectionHeader else {
return nil
}
guard let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderView", for: indexpath) as? HeaderView else {
fatalError()
}
let section = self.dataSource.snapshot().sectionIdentifiers[indexpath.section]
view.sectionTitle.text = section.id.rawValue
view.backgroundColor = .red
return view
}
var snapShot = PropertySnapshot()
snapShot.appendSections(sections)
dataSource.apply(snapShot, animatingDifferences: true)
}
}