Sorry, I made a mistake with my sample. ItemIdentifiers have to be unique and therefore non optional as per my previous comment. But a nil view model mapped from the ItemIdentifier should be supported. This is the updated sample code:
class ViewController: UICollectionViewController {
let crash: Bool = true
@ViewLoading var dataSource: UICollectionViewDiffableDataSource<Int, Int>
override func viewDidLoad() {
super.viewDidLoad()
let registration = UICollectionView.CellRegistration<UICollectionViewListCell, Int?> { cell, indexPath, itemIdentifier in
var config = cell.defaultContentConfiguration()
config.text = itemIdentifier.map { String($0) } ?? "nil"
cell.contentConfiguration = config
}
dataSource = UICollectionViewDiffableDataSource(collectionView: self.collectionView) { [crash] collectionView, indexPath, itemIdentifier in
let model: Int? = if crash {
nil
} else {
itemIdentifier
}
return collectionView.dequeueConfiguredReusableCell(using: registration, for: indexPath, item: model)
}
var snapshot: NSDiffableDataSourceSnapshot<Int, Int> = .init()
snapshot.appendSections([0])
snapshot.appendItems([1,2,3], toSection: 0)
dataSource.apply(snapshot, animatingDifferences: true)
}
}
Topic:
UI Frameworks
SubTopic:
UIKit
Tags: