Daniel Daniel - 2 months ago 19
Swift Question

UICollectionView.dequeueReusableCell crashes

In

viewDidLoad
I register the cell like this:

let cellIdentifier = "Cell"

override func viewDidLoad() {
super.viewDidLoad()

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


and in
cellForItemAt
of the
UICollectionViewDataSource
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
ViewCell
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:],
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UI‌​Kit-3600.7.47/UIColl‌​ectionView.m:5106


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