Swift – Impossible to use “registerNib” on my tableView to add a custom cell

i0S Swift Issue

Question or problem in the Swift programming language:

I’m just figuring out how to add custom cells to a tableView in Swift. I watched a lot of tutorials and they all say at some point to use something like tableView.registerNib which is not working for me !

This is the code I’m using in my tableViewController class :

var nib = UINib(nibName: "ViewExerciceCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "ExerciceCell")

When I try to build, I have an error on the second line which says :

What can I do ? All the tutorials and other answers about custom cells are using this code.

Thanks in advance

How to solve the problem:

Solution 1:

I’m using following code inside tableView:cellForRowAtIndexPath function:

var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as UITableViewCell
if cell == nil {
    tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
    cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as UITableViewCell!

return cell

If you have your custom cell in storyboard or .xib file, don’t forget to set Identifier(in this case CustomCell) in Attributes inspector

Solution 2:

After some head-banging I realized my error:

Instead of “forCellReuseIdentifier” I was supposed to use forCellWithReuseIdentifier. With so many things changing with Swift so swiftly, Its hard to keep up with all the changes. Hope this helps.

Solution that worked for me:

self.collectionView!.register(UINib(nibName: "CategoryCVCell", bundle: nil), forCellWithReuseIdentifier: "CategoryCVCell")

Solution 3:

Ok… I don’t know what was the problem, but removing the file reference of my cell.xib in my project and adding it again just solved the problem.
I already had some problems resolved like that in the past.

Thank you all for your quick answers !

Solution 4:

In my case the solution was changing this:

collectionView.register(MovieCollectionViewCell.self, forCellWithReuseIdentifier: "moviesCollectionView")

To this:

collectionView.register(UINib(nibName: "MovieCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "moviesCollectionView")

Solution 5:

I’ve used a generic function , which I’ve called like this.

tableView.register(UINib(nibName: R.reuseIdentifier.uploadDocHeaderCell.identifier, bundle:nil), forCellReuseIdentifier: R.reuseIdentifier.uploadDocHeaderCell.identifier)

The generic class was used from github, you can just use the following function though:

public func register(_ nibResource: Resource)
    where Resource.ReusableType: UICollectionViewCell
    register(UINib(resource: nibResource), forCellWithReuseIdentifier: nibResource.identifier)

The link for the GitHub resource is:

Link attached for reference

Solution 6:

In your viewDidLoad add following lines:

let nibName = UINib(nibName: "cell_name", bundle:nil)
        collectionView.register(nibName, forCellWithReuseIdentifier: "cell_name")

And then in your cellForItemAt method,add the following lines:

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell_name", for: indexPath)as! cell_name

Make sure to give the identifier to the cell.

Hope this helps!