Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a default UICollectionViewCell is missing a reuseIdentifier

Tags:

ios

swift

If i try to return the default UICollectionViewCell the app crashes because it is missing a reuseIdentifier:

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:'

The code:

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

     return UICollectionViewCell()
}

With UITableViewCells it's working just fine. What is the best practice for default returns in UICollectionViews?

like image 504
Jonas Avatar asked Sep 12 '19 12:09

Jonas


Video Answer


1 Answers

What works for me is:

collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "default")

and then:

return collectionView.dequeueReusableCell(withReuseIdentifier: "default", for: indexPath)

as the default return. But i was hoping for something cleaner.

like image 108
Jonas Avatar answered Oct 07 '22 23:10

Jonas