<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhone Development Blog &#187; Interface Builder</title>
	<atom:link href="http://iPhoneIncubator.com/blog/category/interface-builder/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneIncubator.com/blog</link>
	<description>Tips and Tricks for iPhone, iPod, iPad and iOS Developers</description>
	<lastBuildDate>Mon, 23 Jan 2012 16:49:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>UITableView and Memory</title>
		<link>http://iPhoneIncubator.com/blog/interface-builder/uitableview-and-memory</link>
		<comments>http://iPhoneIncubator.com/blog/interface-builder/uitableview-and-memory#comments</comments>
		<pubDate>Tue, 27 Apr 2010 19:11:31 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Memory Management]]></category>
		<category><![CDATA[CellIdentifier]]></category>
		<category><![CDATA[dequeueReusableCellWithIdentifier]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITableViewCell]]></category>
		<category><![CDATA[UITableViewController]]></category>

		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=475</guid>
		<description><![CDATA[The UITableView class is a wonder of efficient memory management, if you use it correctly.
Here&#8217;s the standard template code that Xcode generates when you create a subclass of UITableViewController:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell [...]<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2011 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/interface-builder/uitableview-and-memory">UITableView and Memory</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The UITableView class is a wonder of efficient memory management, if you use it correctly.</p>
<p>Here&#8217;s the standard template code that Xcode generates when you create a subclass of UITableViewController:</p>
<pre name="code" class="c">// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...

    return cell;
}
</pre>
<p>The keys here are the CellIdentifier variable and the call to dequeueReusableCellWithIdentifier, which enable the iPhone OS to reuse existing instances of UITableViewCell whenever possible.</p>
<p>(Don&#8217;t create a unique reuse identifier for each row as I&#8217;ve seen some developers do. Yes, it&#8217;s much easier to deal with asynchronous download of images for each row if you know how to uniquely identify the cell, and you know that the cell is still in memory. But that totally defeats the efficient memory management that UITableView is capable of.)</p>
<p>Under normal circumstances a UITableView will create one instance of a UITableViewCell per row that is visible on the screen. As you scroll, the cell instance that just rolled off the screen will be reused for the cell that is about to appear.</p>
<p>To verify that this memory management is working as it should, add a log statement each time a new cell is created:</p>
<pre name="code" class="c">if (cell == nil) {
    DLog(@"creating a new cell");
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
</pre>
<p>When you run your app and start scrolling your table view, you should not see any creation of cells beyond the initial list (plus one). If you see &#8220;creating a new cell&#8221; log statements scrolling off the screen as you scroll the table view, you&#8217;ve got a problem.</p>
<p>If you just follow the standard Xcode template above, you should be fine. However if you&#8217;re loading a Nib for a custom table view cell layout using <a href="http://developer.apple.com/iphone/prerelease/library/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW20">Apple&#8217;s recommended way</a>, there&#8217;s an important detail you must not forget. (Tip of the hat to <a href="http://iphonedevelopment.blogspot.com/2010/04/table-view-cells-redux.html">Jeff LaMarche</a> for inspiring this blog post.)</p>
<p>Here&#8217;s the typical NIB loading code from Apple:</p>
<pre name="code" class="c">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CheckedTableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        DLog(@"creating a new cell");

        // Load the table view cell from a Nib file.
        [[NSBundle mainBundle] loadNibNamed:@"CheckedTableViewCell" owner:self options:nil];

        // The checkedTableViewCell property is just a temporary placeholder for loading the Nib.
        cell = checkedTableViewCell;

        // We don't need this anymore, so set to nil.
        self.checkedTableViewCell = nil;
    }

    return cell;
}
</pre>
<p>The key here is that the CellIdentifier value must also be entered into Interface Builder, like this:</p>
<p><img src="http://iPhoneIncubator.com/blog/wp-content/uploads/2010/04/UITableViewCell-Identifier.png" alt="UITableViewCell-Identifier.png" border="0" width="289" height="90" /></p>
<p>If you don&#8217;t do this, then UITableViewCells will not be reused. (A telltale sign of this is that you&#8217;ll see lots of &#8220;creating a new cell&#8221; log messages.) There is no compiler or runtime warning if you fail to enter this critical piece of information into Interface Builder. So that log statement can be a useful warning.</p>
<p>(BTW, if you&#8217;re wondering what DLog is, then see this post: <a href="http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog">The Evolution of a Replacement for NSLog</a>.)</p>
<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2011 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/interface-builder/uitableview-and-memory">UITableView and Memory</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneIncubator.com/blog/interface-builder/uitableview-and-memory/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/7 queries in 0.009 seconds using disk: basic
Object Caching 258/270 objects using disk: basic

Served from: iphoneincubator.com @ 2012-02-07 03:59:48 -->
