ios - How do you load custom UITableViewCells from Xib files?

ID : 10432

viewed : 18

Tags : iosuitableviewobjectcocoa-touchxibios

Top 5 Answer for ios - How do you load custom UITableViewCells from Xib files?

vote vote

97

The right solution is this:

- (void)viewDidLoad {     [super viewDidLoad];     UINib *nib = [UINib nibWithNibName:@"ItemCell" bundle:nil];     [[self tableView] registerNib:nib forCellReuseIdentifier:@"ItemCell"]; }  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     // Create an instance of ItemCell     PointsItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];      return cell; } 
vote vote

87

Here are two methods which the original author states was recommended by an IB engineer.

See the actual post for more details. I prefer method #2 as it seems simpler.

Method #1:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];     if (cell == nil) {         // Create a temporary UIViewController to instantiate the custom cell.         UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"BDCustomCell" bundle:nil];         // Grab a pointer to the custom cell.         cell = (BDCustomCell *)temporaryController.view;         [[cell retain] autorelease];         // Release the temporary UIViewController.         [temporaryController release];     }      return cell; } 

Method #2:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];     if (cell == nil) {         // Load the top-level objects from the custom cell XIB.         NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil];         // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).         cell = [topLevelObjects objectAtIndex:0];     }      return cell; } 

Update (2014): Method #2 is still valid but there is no documentation for it anymore. It used to be in the official docs but is now removed in favor of storyboards.

I posted a working example on Github:
https://github.com/bentford/NibTableCellExample

edit for Swift 4.2

override func viewDidLoad() {     super.viewDidLoad()      // Do any additional setup after loading the view.     self.tblContacts.register(UINib(nibName: CellNames.ContactsCell, bundle: nil), forCellReuseIdentifier: MyIdentifier) }  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {      let cell = tableView.dequeueReusableCell(withIdentifier: MyIdentifier, for: indexPath) as! ContactsCell      return cell } 
vote vote

77

Register

After iOS 7, this process has been simplified down to (swift 3.0):

// For registering nib files tableView.register(UINib(nibName: "MyCell", bundle: Bundle.main), forCellReuseIdentifier: "cell")  // For registering classes tableView.register(MyCellClass.self, forCellReuseIdentifier: "cell") 

(Note) This is also achievable by creating the cells in the .xib or .stroyboard files, as prototype cells. If you need to attach a class to them, you can select the cell prototype and add the corresponding class (must be a descendant of UITableViewCell, of course).

Dequeue

And later on, dequeued using (swift 3.0):

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)      cell.textLabel?.text = "Hello"      return cell } 

The difference being that this new method not only dequeues the cell, it also creates if non-existant (that means that you don't have to do if (cell == nil) shenanigans), and the cell is ready to use just as in the example above.

(Warning) tableView.dequeueReusableCell(withIdentifier:for:) has the new behavior, if you call the other one (without indexPath:) you get the old behavior, in which you need to check for nil and instance it yourself, notice the UITableViewCell? return value.

if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MyCellClass {     // Cell be casted properly     cell.myCustomProperty = true } else {     // Wrong type? Wrong identifier? } 

And of course, the type of the associated class of the cell is the one you defined in the .xib file for the UITableViewCell subclass, or alternatively, using the other register method.

Configuration

Ideally, your cells have been already configured in terms of appearance and content positioning (like labels and image views) by the time you registered them, and on the cellForRowAtIndexPath method you simply fill them in.

All together

class MyCell : UITableViewCell {     // Can be either created manually, or loaded from a nib with prototypes     @IBOutlet weak var labelSomething : UILabel? = nil }  class MasterViewController: UITableViewController  {     var data = ["Hello", "World", "Kinda", "Cliche", "Though"]      // Register     override func viewDidLoad()     {         super.viewDidLoad()          tableView.register(MyCell.self, forCellReuseIdentifier: "mycell")         // or the nib alternative     }      override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int     {         return data.count     }      // Dequeue     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {         let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! MyCell          cell.labelSomething?.text = data[indexPath.row]          return cell     } } 

And of course, this is all available in ObjC with the same names.

vote vote

66

Took Shawn Craver's answer and cleaned it up a bit.

BBCell.h:

#import <UIKit/UIKit.h>  @interface BBCell : UITableViewCell { }  + (BBCell *)cellFromNibNamed:(NSString *)nibName;  @end 

BBCell.m:

#import "BBCell.h"  @implementation BBCell  + (BBCell *)cellFromNibNamed:(NSString *)nibName {     NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL];     NSEnumerator *nibEnumerator = [nibContents objectEnumerator];     BBCell *customCell = nil;     NSObject* nibItem = nil;     while ((nibItem = [nibEnumerator nextObject]) != nil) {         if ([nibItem isKindOfClass:[BBCell class]]) {             customCell = (BBCell *)nibItem;             break; // we have a winner         }     }     return customCell; }  @end 

I make all my UITableViewCell's subclasses of BBCell, and then replace the standard

cell = [[[BBDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BBDetailCell"] autorelease]; 

with:

cell = (BBDetailCell *)[BBDetailCell cellFromNibNamed:@"BBDetailCell"]; 
vote vote

54

I used bentford's Method #2:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];     if (cell == nil) {         // Load the top-level objects from the custom cell XIB.         NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil];         // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).         cell = [topLevelObjects objectAtIndex:0];     }      return cell; } 

It works, but watch out for connections to File's Owner in your custom UITableViewCell .xib file.

By passing owner:self in your loadNibNamed statement, you set the UITableViewController as File's Owner of your UITableViewCell.

If you drag and drop to the header file in IB to set up actions and outlets, it will set them up as File's Owner by default.

In loadNibNamed:owner:options, Apple's code will try to set properties on your UITableViewController, since that's the owner. But you don't have those properties defined there, so you get an error about being key value coding-compliant:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason:     '[<MyUITableViewController 0x6a383b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key myLabel.' 

If an Event gets triggered instead, you'll get an NSInvalidArgumentException:

-[MyUITableViewController switchValueDidChange:]: unrecognized selector sent to instance 0x8e9acd0 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyUITableViewController switchValueDidChange:]: unrecognized selector sent to instance 0x8e9acd0' *** First throw call stack: (0x1903052 0x15eed0a 0x1904ced 0x1869f00 0x1869ce2 0x1904ec9 0x5885c2 0x58855a 0x62db76 0x62e03f 0x77fa6c 0x24e86d 0x18d7966 0x18d7407 0x183a7c0 0x1839db4 0x1839ccb 0x1f8b879 0x1f8b93e 0x585a9b 0xb904d 0x2c75) terminate called throwing an exceptionCurrent language:  auto; currently objective-c 

An easy workaround is to point your Interface Builder connections at the UITableViewCell instead of File's Owner:

  1. Right click on File's Owner to pull up the list of connections
  2. Take a screen capture with Command-Shift-4 (drag to select the area to be captured)
  3. x out the connections from File's Owner
  4. Right click on the UITableCell in the Object hierarchy and re-add the connections.

Top 3 video Explaining ios - How do you load custom UITableViewCells from Xib files?

Related QUESTION?