Jonas
Jonas

Reputation: 2249

Returning a default UICollectionViewCell is missing a reuseIdentifier

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?

Upvotes: 10

Views: 4580

Answers (1)

Jonas
Jonas

Reputation: 2249

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.

Upvotes: 10

Related Questions