Swift - Impossible to use "registerNib" on my tableView to add a custom cell
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
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<Resource: NibResourceType & ReuseIdentifierType>(_ nibResource: Resource)
where Resource.ReusableType: UICollectionViewCell
{
register(UINib(resource: nibResource), forCellWithReuseIdentifier: nibResource.identifier)
}
The link for the GitHub resource is:
Solution 5
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")
![Nicolas Meienberger](https://i.stack.imgur.com/muCHa.jpg?s=256&g=1)
Nicolas Meienberger
Updated on July 24, 2022Comments
-
Nicolas Meienberger almost 2 years
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 :
Cannot invoke 'registerNib' with an argument list of type '(UINib, forCellReuseIdentifier: String)'.
What can I do ? All the tutorials and other answers about custom cells are using this code.
Thanks in advance
-
drew.. over 6 yearsSeems wasteful to register the Nib over and over here. Do it once, on load.
-
Bruce Patin over 5 yearsThat's funny. When I tried to use "forCellWithReuseIdentifier", I got an error and had to change it back to "forCellReuseIdentifier", not that either one fixed the problem.
-
N a y y a r about 4 yearsI second @drew..'s comment. Registring a cell is just a one-time requirement. So doesn't require to be written within
cellForRowAtIndexPath:
method.