Here is the error:
Thread 1: "Attempted to dequeue a cell using a registration that was created inside -collectionView:cellForItemAtIndexPath: or inside a UICollectionViewDiffableDataSource cell provider. Creating a new registration each time a cell is requested will prevent reuse and cause created cells to remain inaccessible in memory for the lifetime of the collection view. Registrations should be created up front and reused. Registration: <UICollectionViewCellRegistration: 0x60000135b300>"
Here is file that is causing the error:
import UIKit
class PetExplorerViewController: UICollectionViewController {
// MARK: - Properties
var adoptions = Set<Pet>()
lazy var dataSource = makeDataSource()
// MARK: - Types
enum Section: Int, CaseIterable, Hashable {
case availablePets
case adoptedPets
}
typealias DataSource = UICollectionViewDiffableDataSource<Section, Item>
// MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Pet Explorer"
configureLayout()
applyInitialSnapshots()
}
// MARK: - Functions
func configureLayout() {
let configuration = UICollectionLayoutListConfiguration(appearance: .grouped)
collectionView.collectionViewLayout = UICollectionViewCompositionalLayout.list(using: configuration)
}
func applyInitialSnapshots() {
var categorySnapshots = NSDiffableDataSourceSnapshot<Section, Item>()
let categories = Pet.Category.allCases.map { category in
return Item(title: String(describing: category))
}
categorySnapshots.appendSections([.availablePets])
categorySnapshots.appendItems(categories, toSection: .availablePets)
dataSource.apply(categorySnapshots, animatingDifferences: false)
}
func makeDataSource() -> DataSource {
return DataSource(collectionView: collectionView) {collectionView, indexPath, item in
return collectionView.dequeueConfiguredReusableCell(using: self.categoryCellRegistration(), for: indexPath, item: item)
}
}
}
// MARK: - CollectionView Cells
extension PetExplorerViewController {
}
// MARK: - UICollectionViewDelegate
extension PetExplorerViewController {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
func pushDetailForPet(_ pet: Pet, withAdoptionStatus isAdopted: Bool) {
let storyboard = UIStoryboard(name: "Main", bundle: .main)
let petDetailViewController =
storyboard.instantiateViewController(identifier: "PetDetailViewController") { coder in
return PetDetailViewController(coder: coder, pet: pet)
}
petDetailViewController.delegate = self
petDetailViewController.isAdopted = isAdopted
navigationController?.pushViewController(petDetailViewController, animated: true)
}
}
// MARK: - PetDetailViewControllerDelegate
extension PetExplorerViewController: PetDetailViewControllerDelegate {
func petDetailViewController(_ petDetailViewController: PetDetailViewController, didAdoptPet pet: Pet) {
}
func categoryCellRegistration() -> UICollectionView.CellRegistration<UICollectionViewListCell, Item> {
return .init { cell, _, item in
var configuration = cell.defaultContentConfiguration()
configuration.text = item.title
cell.contentConfiguration = configuration
}
}
}
I think that should be all that's needed, but would be more than willing to add any other relevant needed for troubleshooting.