Daniel Daniel - 1 year ago 68
Swift Question

UICollectionView.dequeueReusableCell crashes


I register the cell like this:

let cellIdentifier = "Cell"

override func viewDidLoad() {

let cellNib = UINib(nibName: "ViewCell", bundle: nil)
collection.register(cellNib, forCellWithReuseIdentifier: cellIdentifier)

and in
of the
I do:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// It crashes while trying to dequeue with the Error message: Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:]
let dequedCell = collection.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)
return dequedCell

The onlystrange thing might be that the code is Mixed Swift/ObjC, and
is ObjC and gets imported in the bridging header:

#import "ViewCell.h"

I am getting the following error:

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewC‌​ategory:],

I made sure all names are correct. Does anyone know why this crash is happening??

Answer Source

Here is problem

1) You need to register cell identifier in XIB,

2) Assign Class name to XIB

3) cell for row you need to check with as? YourClass in dequeue and

4) dequeue with same identifier you assign to XIB and register

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download