1

I am adding a collection view programmatically. And every time I try to return a cell, I keep getting this error:

*** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3698.140/UICollectionView.m:2022

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier - cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:'

But I am using the correct identifier to dequeue it. I register it in viewDidLoad. I couldn't find anything online.

Registration:

   view.backgroundColor = .white
        reports = readData()
        filterOptionsView.delegate = self
        filterOptionsView.dataSource = self
        filterOptionsView.register(OptionsCell.self, forCellWithReuseIdentifier: OptionsCell.identifier)
        if let layout = filterOptionsView.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .horizontal
        }

Dequeuing:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if(collectionView == filterOptionsView) {
        let optionsCell = collectionView.dequeueReusableCell(withReuseIdentifier: OptionsCell.identifier, for: indexPath) as! OptionsCell

        return optionsCell.configureCell(option: tags[indexPath.row], selected: selectedTag == tags[indexPath.row])
    }

    return UICollectionViewCell()
}
Parth
  • 2,682
  • 1
  • 20
  • 39

0 Answers0