I have set up a collection view cell programmatically which I am registering in the cell registration of a diffable data source. I have set up a delegate for the cell, and the view controller which contains the collection view as the delegate for the cell. However, calling the cell delegate method from the cell is not calling the method in the view controller.
This is my code:
`// in my view controller: cell.delegate = self
// in my collection view cell: delegate?.repromptLLMForIconName(iconName, index: index, emotion: emotion, red: red, green: green, blue: blue)
`
But although the delegate method in the collection view gets called, my method implementation in the view controller does not. What could possibly be going wrong?
I ended up resolving this by setting cell.delegate = self
in the dataSource
handler where I connect the data source to my collection view in EmotionExplorerViewController
.
// Create diffable data source
dataSource = UICollectionViewDiffableDataSource<Section, Item>(
collectionView: emotionExplorerCollectionView
) {
(collectionView, indexPath, item) -> UICollectionViewCell? in
// Return the appropriate cell based on the item type
switch item {
case .title:
return collectionView.dequeueConfiguredReusableCell(
using: titleCellRegistration,
for: indexPath,
item: item
)
case .main:
let cell = collectionView.dequeueConfiguredReusableCell(
using: mainCellRegistration,
for: indexPath,
item: item
)
cell.delegate = self
return cell
case .filter:
return collectionView.dequeueConfiguredReusableCell(using: filterCellRegistration,
for: indexPath,
item: item)
case .skeleton:
return collectionView.dequeueConfiguredReusableCell(using: skeletonCellRegistration,
for: indexPath,
item: item)
}
}
I also reverted to using my old cell registration method since the content configuration API was a bit tricky to set up and customise.