0

I am making a custom collectionview cell Using XIB.

The collectionview is placed inside an viewController as an extension.

This is the code i am using to call the Xib View but i get an error telling me i need to use reuseidentifier. But i have no clue how to use that while using XIB.

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

        let cell = Bundle.main.loadNibNamed("CustomCell", owner: self, options: nil)?.first as! CustomCell

        return cell
    }

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:' *** First throw call stack:

Asbis
  • 432
  • 5
  • 16

3 Answers3

2

First you need to create a reuseIdentifier for your cell. Lets create it based on your collectionViewCell class name. Declare reuseId, in your ViewController file:

let reuseId = String(describing: CustomCell.self)

You need to register your cell to your collectionView in viewDidLoad method.

collectionView.register(CustomCell.self, forCellReuseIdentifier: reuseId)

Then in your cellForItemAt method:

guard let cell = collectionView.dequeueReusableCell(withIdentifier: reuseId, for: indexPath) as? CustomCell else { return UICollectionViewCell() }
//return cell, or update cell elements first.
emrepun
  • 2,496
  • 2
  • 15
  • 33
1

You can register the CustomCell like,

let customCellNib = UINib(nibName: "CustomCell", bundle: .main)
collectionView.register(customCellNib, forCellWithReuseIdentifier: "CustomCell")

And use the same registered cell in cellForItemAt like,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"CustomCell", for: indexPath) as? CustomCell else {
        return UICollectionViewCell()
    }
    return cell
}
Bappaditya
  • 9,494
  • 3
  • 20
  • 29
  • Tank you for your answer. I have another question. What if i have 2 different customCell? let say CustomCell1 and CustomCell2. How do i register them? – Asbis Dec 22 '18 at 15:17
  • same `let customCellNib1 = UINib(nibName: "CustomCell1", bundle: .main) collectionView.register(customCellNib1, forCellWithReuseIdentifier: "CustomCell1")` `let customCellNib2 = UINib(nibName: "CustomCell2", bundle: .main) collectionView.register(customCellNib2, forCellWithReuseIdentifier: "CustomCell2")` And based on your requirement or logic the same specific cell should be returned in `cellForItemAt` delegate method. – Bappaditya Dec 22 '18 at 16:49
1

For Swift 4.0 and 4.2

In your viewDidLoad:

custom collectionViewCell

mainCollectionView.register(UINib(nibName: "your_custom_cell_name", bundle: nil), forCellWithReuseIdentifier: "your_custom_cell_identifier")

In cellForItemAt indexPath:

let cell : <your_custom_cell_name> = mainCollectionView.dequeueReusableCell(withReuseIdentifier: "your_custom_cell_identifier", for: indexPath) as! <your_custom_cell_name>

And don't forget to set identifier for your custom cell in xib section.

Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43