UICollectionView.CellRegistration from custom nib

I'm trying to migrate from old style cell registration collectionView?.register(UINib(nibName: "MyCustomViewCell", bundle: nil), forCellWithReuseIdentifier: MyCustomViewCell.reuseIdentifier) to new UICollectionView.CellRegistration.

Currently I always end up with an NSInternalConstistencyException: view reuse identifier in nib (MyCustomViewCell) does not match the identifier used to register the nib (9AA266FF-550D-4A6B-ACCD-4E90B25E6DDC).

This is how I do my new style cell registration:

let cellRegistration = UICollectionView.CellRegistration<MyCustomViewCell, MyItem>(cellNib: UINib(nibName: "MyCustomViewCell", bundle: nil)) { cell, indexPath, item in
            cell.configure(with: item)
        }

If I use a default cell to test the whole collectionView setup everything works as expected (I see my cells etc.):

        let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, MyItem> { cell, indexPath, item in
           // Populate the cell with our item description.
            var contentConfiguration = cell.defaultContentConfiguration()
            contentConfiguration.text = item.title
        }

My custom cell has a reuseIdentifier both in the nib file as well as in the class definition eg.:

class MyCustomViewCell: UICollectionViewCell {
    static let reuseIdentifier = "MyCustomViewCell"

Any hints?

Answered by b00tsyb00ts in 689569022

Turns out it was a problem with the nib file, which was not a problem for the "old way" of registering nibs.

I recreated a new swift class + xib file and then it worked. Couldn't see what the difference was though...

Accepted Answer

Turns out it was a problem with the nib file, which was not a problem for the "old way" of registering nibs.

I recreated a new swift class + xib file and then it worked. Couldn't see what the difference was though...

You should keep Reuse identifier empty in your xib file

The fix pointed out bei uni.nova.hero worked for me.

UICollectionView.CellRegistration from custom nib
 
 
Q