Prototype Cells in a nib instead of a storyboard
iOS 5 includes a new method on UITableView: registerNib:forCellReuseIdentifier:
To use it, put a UITableViewCell in a nib. It has to be the only root object in the nib.
You can register the nib after loading your tableView, then when you call dequeueReusableCellWithIdentifier: with the cell identifier, it will pull it from the nib, just like if you had used a Storyboard prototype cell.
In Swift 4:
- Create new CocoaTouch Class, Subclass from UITableViewCell.
- Important - Enable the checkbox 'Also create XIB file' which creates a Swift file and a .xib file
- Add labels in the Xib, as you would do for a prototype cell in Storyboard
- Connect label outlets to the new swift file
-
Important - Register the nib file in viewDidLoad
yourTableview.register(UINib.init(nibName: "CustomCellTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell")
(or)
yourTableview.register(UINib(nibName: "CustomCellTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "Cell")
- Implement the datasource and delegates as normal and typecaste to the CustomCell in cellForRowAtIndexPath.