59

For better re-usability I want to create a table view outside of my Storyboard.

Now when I create a UITableView based ViewController with Nib in Xcode I get the default TableView in the nib file. However, I am not able in Interface Builder to add prototype cells like I am in my Storyboard.

Is it currently not possible to add prototype cells in a nib or am I missing something.

Thanks very much for any help.

Besi
  • 22,579
  • 24
  • 131
  • 223
  • 1
    In addition to correct answer above I would refer to great detailed solution for this case. - [Link to StackAnswer](http://stackoverflow.com/a/10298648/1698467) (by using `UITableView: registerNib:forCellReuseIdentifier:` and `tableView:cellForRowAtIndexPath:` with `.xib` files) – skywinder Sep 20 '13 at 17:01
  • This question/answer is a bit confusing to me. I came here looking for a way to use prototype cells within a table view when defining a table view in a xib. It's perfectly possible in a storyboard yes, but somehow within a xib there just is no such feature. – Jonny May 19 '17 at 04:43

2 Answers2

92

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.

Richard Venable
  • 8,310
  • 3
  • 49
  • 52
  • 1
    Fantastic - I was just wondering how to do this, searched here, saw your answer, then saw that mine (which turns out to be wrong!) was the accepted answer. Besi, you should accept this one instead. – jrturton Feb 05 '12 at 19:20
  • 2
    Is there any way to set the file owner of these cells to be the controller where they are registered from? – nicktmro Apr 16 '12 at 01:24
  • Thanks for this solution. This is probably the best way for reusable tableview cells for storyboards I have come across so far. – Ondrej Peterka Jul 14 '12 at 16:14
  • It's very useful! I'm so glad having this method solve same problem. – Matt.Z Aug 02 '12 at 07:00
  • 3
    The only downside is you have to wire up all the action selectors yourself, which I'm just doing by calling addTarget during willDisplayCell and then calling removeTarget during the cell's prepareForReuse method. Is there something more clever I could be doing, that would let you wire things in storyboard? – Duane Fields Sep 21 '12 at 15:34
  • Thanks for mentioning this: "It has to be the only root object in the nib." I was trying to combine my tableviewcell and view nibs together but couldn't find any info about how `UINib` would find my tableviewcell. That settled it! – cbowns May 16 '13 at 00:03
  • Can I ask - what if you don't want to use a custom .nib ? Supposing I just want basic UITableViewCells with a textLabel. What reuseIdentifier name should I specify in the "cellForRowAtIndexPath" function ? Unlike UITableViews in a Storyboard, there is nowhere for me to define the reuseIdentifier, when the UITableViewController is in it's own .xib file.. – Mike Gledhill Jun 27 '14 at 10:57
  • @MikeGledhill you can define the reuse identifier in a xib the exact same way as in a Storyboard file. – Richard Venable Jul 07 '14 at 20:48
  • Link doesn't work any more, here's an updated one: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instm/UITableView/registerNib:forCellReuseIdentifier: – Bek Oct 29 '14 at 14:32
7

In Swift 4:

  1. Create new CocoaTouch Class, Subclass from UITableViewCell.
  2. Important - Enable the checkbox 'Also create XIB file' which creates a Swift file and a .xib file
  3. Add labels in the Xib, as you would do for a prototype cell in Storyboard
  4. Connect label outlets to the new swift file
  5. 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")
    
  6. Implement the datasource and delegates as normal and typecaste to the CustomCell in cellForRowAtIndexPath.
Naishta
  • 11,885
  • 4
  • 72
  • 54