DiffableCollectionViewDataSource with different cell registrations

263 views Asked by At

I've been trying to make a section for diffable collection view data source that could have a list of GenericItem where this class would have a property cellRegistration that would be of type UICollectionView.CellRegistration. After this class every other item would be inheriting, something like that:

class GenericItem:  Hashable {
    
    var id = UUID()
    
    var cellRegistration: UICollectionView.CellRegistration<UICollectionViewCell, GenericItem> {
        UICollectionView.CellRegistration<UICollectionViewCell, GenericItem> { _, _, _ in }
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
    
    static func == (lhs: GenericItem, rhs: GenericItem) -> Bool {
        lhs.id == rhs.id
    }
}

final class SomeItem: GenericItem {
    
    let title = "Some title"
    
    override var cellRegistration: UICollectionView.CellRegistration<UICollectionViewCell, GenericItem> {
        UICollectionView.CellRegistration<SomeCollectionViewCell, SomeItem> { cell, indexPath, item in
            cell.populate(with: item)
        }
    }
}

So that I would be able to put into this list any cell type I want and don't worry about checking in data source which item type it is and then use corresponding CellRegistration. But I must be overlooking/misunderstanding something because no matter how I try to rewrite it I pretty much end up with the same error: Cannot convert return expression of type 'UICollectionView.CellRegistration<SomeCollectionViewCell, SomeItem>' to return type 'UICollectionView.CellRegistration<UICollectionViewCell, GenericItem>'

Could someone explain how to go about creating such a system with this new API?

Thanks!

0

There are 0 answers