Swift Cannot use "registerNib" in my table view to add a custom cell

I will just figure 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 doesn't work for me!

This is the code I use in the tableViewController class:

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

When I try to build, an error appears in the second line:

Cannot call 'registerNib' using argument list of type '(UINib, forCellReuseIdentifier: String)'.

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

Thanks in advance

+8
xcode uitableview swift cell
source share
3 answers

I use the following code inside the tableView:cellForRowAtIndexPath function tableView:cellForRowAtIndexPath :

 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 a custom cell in the storyboard or .xib file, be sure to set the Identifier (in this case CustomCell) in the attribute inspector

+6
source share

Ok ... I donโ€™t know what the problem is, but deleting the link to my cell.xib file in my project and adding it again just solved the problem. I already had problems that were resolved in the past.

Thank you all for your quick answers!

+2
source share

After some headbutt, I realized my mistake:

Instead of "forCellReuseIdentifier" I had to use forCellWithReuseIdentifier. With so many things that change quickly with Swift, it's 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") 
+1
source share

All Articles