SIGABRT: could not dequeue a view of kind: UICollectionElementKindCell with identifier KPICollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

Hi Team,

The app is crashing on iOS 15.4.1 and iOS 15.3.1 Please check the below crash reason:- SIGABRT: could not dequeue a view of kind: UICollectionElementKindCell with identifier KPICollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

I have written the below code but am unable to understand what is wrong with this:-

guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TableItemCollectionViewCell.identifier, for: indexPath) as? TableItemCollectionViewCell
else { return collectionView.emptyCell(at: indexPath) }

Replies

Please tell what identifiers you have given to TableItemCollectionViewCell and to UICollectionElementKindCell.

Thanks for the quick reply here.

 static let identifier = "TableItemCollectionViewCell"

I have used "TableItemCollectionViewCell" identifier.

Could you explain how tou defined the cells ?

I understand you have

  • declared TableItemCollectionViewCell as a subclass of UITableViewCell.
  • created a nib for the cell. I assume the nib file name is TableItemCollectionViewCell.xib

Exact ?

If so, you have to register the the nib in the tableView, in viewDidLoad:

        static let identifier = "TableItemCollectionViewCell"
        let nibName = "TableItemCollectionViewCell"  // Probably the same as identifier
        let nib = UINib(nibName: nibName, bundle: nil) 

        tableView.register(nib, forCellReuseIdentifier: identifier)

My app is working before iOS 15.2 version and now crashlytics capture the few crashes which is related to this thread.

Please check the code which I have written:-

static var nib: UINib { return UINib(nibName: String(describing: self), bundle: Bundle(for: Self.self).resource) }

static let identifier = "TableItemCollectionViewCell"

// cell register

 override func awakeFromNib() {

    super.awakeFromNib()

    collectionView.register(TableItemCollectionViewCell.nib,                 forCellWithReuseIdentifier: TableItemCollectionViewCell.identifier)

}

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

  guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TableItemCollectionViewCell.identifier,                               for: indexPath) as? TableItemCollectionViewCell,        let cellVM = viewModel.cellViewModel(for: indexPath,                            isHorizontalScrollingEnabled: isHorizontalScrollingEnabled, collectionViewWidth: collectionView.frame.size.width) as TableItemCellViewModel?

else {

return collectionView.emptyCell(at: indexPath)

}

// do code here

 cell.configure(with: cellVM)

}

I usually register in viewDidLoad.

I find the declaration of nib uselessly complex, but if it works…

Hi Team,

Can someone help here to identify the root cause?