Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here are two methods which the <a href="http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/" rel="noreferrer">original author states was recommended by an IB engineer</a>.</p> <p>See the actual post for more details. I prefer method #2 as it seems simpler.</p> <p><strong>Method #1:</strong></p> <pre><code>- (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; } </code></pre> <p><strong>Method #2:</strong></p> <pre><code>- (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; } </code></pre> <p><strong>Update (2014):</strong> Method #2 is still valid but there is no documentation for it anymore. It used to be in the <a href="http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html#//apple_ref/doc/uid/TP40007451-CH1-SW1" rel="noreferrer">official docs</a> but is now removed in favor of storyboards.</p> <p>I posted a working example on Github:<br> <a href="https://github.com/bentford/NibTableCellExample" rel="noreferrer">https://github.com/bentford/NibTableCellExample</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload