App Crash when dequeueReusableCell from UICollectionViewCell to UIViewController

I have name the table view cell identifier as cell still keep crashing. Restart XCODE and restart MacBook several times still not working.
  • ********** Codes******

extension ItemViewController: UICollectionViewDataSource, UICollectionViewDelegate {

    

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

        

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ImageCollectionViewCell



        if itemImages.count > 0 {

            cell.setupImageWith(itemImage: itemImages[indexPath.row])

        }



        return cell

    }

***********

  • ********** Crash Error******

2020-06-23 08:52:56.578332+0800 MarketPlaceDev[7650:1771105] * Assertion failure in -[UICollectionView
dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3920.31.101/UICollectionView.m:5972

2020-06-23 08:52:56.579198+0800 MarketPlaceDev[7650:1771105] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

* First throw call stack:

Replies

Hi aulimge,

You need to register your custom cell class ImageCollectionViewCell with the collection view before you can use it. Try adding collectionView.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: "cell") after you create your collection view.