Unable to Dequeue Registered CollectionViewCells

Searched here and there but I couldn't find anything that I haven't already tried to fix my issue, so going to ask it again.

I am trying to use a collection view with custom cells.

The main collection view is in the Main View Controller.

I'm using a generic cell named GenericHomeCollectionViewCell, then customize another collection view that it has. This cell doesn't have any issue with registering and dequeuing. (It might sound weird but I already have an application with this usage and it works without any problem, so I wanted to use the same method again.)

The crash happens when I try to register and dequeue cells inside that generic cell.

I have 4 different custom cells for use which are registered in awakeFromNib().

GenericHomeCollectionViewCell.swift

override func awakeFromNib() {
    super.awakeFromNib()
    
    newsCollectionView.register(UINib(nibName: "HeadlineNewsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "HeadlineNewsCollectionViewCell")
    newsCollectionView.register(UINib(nibName: "HomeAuthorCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "HomeAuthorCollectionViewCell")
    newsCollectionView.register(UINib(nibName: "NewsListCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "NewsListCollectionViewCell")
    newsCollectionView.register(UINib(nibName: "HomeDetailedNewCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "HomeDetailedNewCollectionViewCell")
    
    newsCollectionView.delegate = self
    newsCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if sectionType == .swipe {
        let cell : HeadlineNewsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeadlineNewsCollectionViewCell", for: indexPath) as! HeadlineNewsCollectionViewCell
        cell.prepareCell(news: newsList[indexPath.row])
        return cell
     } else if sectionType == .homeAuthor {
        let cell : HomeAuthorCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeAuthorCollectionViewCell", for: indexPath) as! HomeAuthorCollectionViewCell
        cell.prepareCell(news: newsList[indexPath.row])
        return cell
    } else {
        let cell : HeadlineNewsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeadlineNewsCollectionViewCell", for: indexPath) as! HeadlineNewsCollectionViewCell
        cell.prepareCell(news: newsList[indexPath.row])
        return cell
     }
}

When I launch the application, I get crash with error:

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], UICollectionView.m:6502

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier HeadlineNewsCollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

I tried to give a reuse identifier name from the storyboard editor's but there is no difference.

Replies

Not sure that's the issue, but I usually register in viewDidLoad.

Add some test here:

override func awakeFromNib() {
    super.awakeFromNib()
    print(#function)

And tell if you get the log.

‘awakeFromNib()’ is the equal of ‘viewDidLoad()’ for custom cells (aka XIBs) and I register the cells in where it should be. I already tried debugging and see if the method being called, it actually does run the register steps, but somehow the cells are not registered in the end.

  • Is there a single collectionView (newsCollectionView) in your class ? Could you show the code for HeadlineNewsCollectionViewCell ? Interesting discussion here: https://stackoverflow.com/questions/17729582/uicollectionview-in-a-uicollectionviewcell

  • @Claude31 I have found the problem. I have 2 more CollectionViews in GenericHomeCollectionViewCell; one for numbers under the news and another one for the right menu. When I disable those 2 collection views, the application launched without any problems, and my newsCollectionView populated with the data. I guess the cells were trying to dequeue for those collectionViews, not my newsCollectionView. Any idea how can I work around this?

  • What are the identifiers of those CollectionViews ? Check carefully in all your project for those identifiers, maybe you have some mess up somewhere.

Add a Comment