In the original code, if the pet is adopted, the petCellRegistration function injects a cell.backgroundConfiguration with a color, etc.
This solution moves the adoption decision away from the cell registration function, and places it in the
dataSource factory function.
Modify the code as follows:
func makePetCellRegistration() -> UICollectionView.CellRegistration<UICollectionViewListCell, Item> {
return .init { cell, _, item in
guard let pet = item.pet else {
return
}
var configuration = cell.defaultContentConfiguration()
configuration.text = pet.name
configuration.secondaryText = "\(pet.age) years old"
configuration.image = UIImage(named: pet.imageName)
configuration.imageProperties.maximumSize = CGSize(width: 40, height: 40)
cell.contentConfiguration = configuration
cell.accessories = [.disclosureIndicator()]
}
}
Next, make a new cell registration with a modified background for a pet that is adopted:
func makePetCellWithAdoptedBackgroundRegistration() -> UICollectionView.CellRegistration<UICollectionViewListCell, Item> {
return .init { cell, _, item in
guard let pet = item.pet else {
return
}
var configuration = cell.defaultContentConfiguration()
configuration.text = pet.name
configuration.secondaryText = "\(pet.age) years old"
configuration.image = UIImage(named: pet.imageName)
configuration.imageProperties.maximumSize = CGSize(width: 40, height: 40)
cell.contentConfiguration = configuration
cell.accessories = [.disclosureIndicator()]
// The pet *IS* adopted. Only adopted pets will have a colored background.
var backgroundConfig = UIBackgroundConfiguration.listPlainCell()
backgroundConfig.backgroundColor = .systemBlue
backgroundConfig.cornerRadius = 5
backgroundConfig.backgroundInsets = NSDirectionalEdgeInsets(
top: 5, leading: 5, bottom: 5, trailing: 5)
cell.backgroundConfiguration = backgroundConfig
}
}
Finally, modify makeDataSource to check if the pet is adopted before assigning a cell registration:
func makeDataSource() -> DataSource {
let categoryCellRegistration = makeCategoryCellRegistration()
let petCellRegistration = makePetCellRegistration()
let petCellWithAdoptedBackgroundRegistration = makePetCellWithAdoptedBackgroundRegistration()
let adoptedPetCellRegistration = makeAdoptedPetCellRegistration()
return DataSource(collectionView: collectionView) {
collectionView, indexPath, item -> UICollectionViewCell? in
if let pet = item.pet {
guard let section = Section(rawValue: indexPath.section) else {
return nil
}
switch section {
case .availablePets:
if self.adoptions.contains(pet) {
return collectionView.dequeueConfiguredReusableCell(
using: petCellWithAdoptedBackgroundRegistration, for: indexPath, item: item)
}
else {
return collectionView.dequeueConfiguredReusableCell(
using: petCellRegistration, for: indexPath, item: item)
}
case .adoptedPets:
return collectionView.dequeueConfiguredReusableCell(
using: adoptedPetCellRegistration, for: indexPath, item: item)
}
}
else {
return collectionView.dequeueConfiguredReusableCell(
using: categoryCellRegistration, for: indexPath, item: item)
}
}
}