Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you load custom UITableViewCells from Xib files?
    text
    copied!<p>The question is simple: How do you load custom <code>UITableViewCell</code> from Xib files? Doing so allows you to use Interface Builder to design your cells. The answer apparently is not simple due to memory managment issues. <a href="https://stackoverflow.com/questions/134575/using-interface-builder-for-uitableviews">This thread</a> mentions the issue and suggests a solution, but is pre NDA-release and lacks code. Here's a <a href="http://discussions.apple.com/thread.jspa?messageID=8040526&amp;tstart=0" rel="nofollow noreferrer">long thread</a> that discusses the issue without providing a definitive answer. </p> <p>Here's some code I've used:</p> <pre><code>static NSString *CellIdentifier = @"MyCellIdentifier"; MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]; cell = (MyCell *)[nib objectAtIndex:0]; } </code></pre> <p>To use this code, create MyCell.m/.h, a new subclass of <code>UITableViewCell</code> and add <code>IBOutlets</code> for the components you want. Then create a new "Empty XIB" file. Open the Xib file in IB, add a <code>UITableViewCell</code> object, set its identifier to "MyCellIdentifier", and set its class to MyCell and add your components. Finally, connect the <code>IBOutlets</code> to the components. Note that we did not set the File's Owner in IB.</p> <p>Other methods advocate setting the File's Owner and warn of memory leaks if the Xib is not loaded via an additional factory class. I tested the above under Instruments/Leaks and saw no memory leaks. </p> <p>So what's the canonical way to load cells from Xibs? Do we set File's Owner? Do we need a factory? If so, what's the code for the factory look like? If there are multiple solutions, let's clarify the pros and cons of each of them...</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