func configureDataSource() {
print("configure dataSource!!!")
dataSource = UICollectionViewDiffableDataSource
<Section, StoryItem>(collectionView: storyCollectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, storyItem: StoryItem) -> UICollectionViewCell? in
print("try creating a collection view cell!")
The print statement showing that the function was called appears in the console, but the code within the { for UICollectionViewDiffableDataSource
does not run.
Any suggestions on where to troubleshoot next? Thanks so much!
There needs to be a lot more code than that. You need to populate the diffable data source with some data. Otherwise, the number of items in your collection view is zero and there is no need to generate any cells.
A typical dance (usually in viewDidLoad
) will go something like this:
// make the data source
self.datasource = UICollectionViewDiffableDataSource<String,String>(collectionView:self.collectionView) {
cv,ip,s in
return self.makeCell(cv,ip,s) // return cell
}
// give it a supplementary view provider if you have headers/footers
self.datasource.supplementaryViewProvider = { cv,kind,ip in
return self.makeSupplementaryView(cv,kind,ip) // return view
}
// give the data source some data (here, my data is sitting in `sections`)
var snap = NSDiffableDataSourceSnapshot<String,String>()
for section in sections {
snap.appendSections([section.0])
snap.appendItems(section.1)
}
self.datasource.apply(snap, animatingDifferences: false)