Fixing explosions caused by UITableView scrolling

Feb 11, 2010 20:36 · 307 words · 2 minute read iphone objective-C xcode

I came across a weird (well, weird to me) problem with a UITableView this evening.   Basically, the situation is this:

I’ve got a Tab Bar controller in my app’s main window; with three tabs:

[![Screen shot 2010-02-11 at 20.31.52](http://www.adoptioncurve.net/archives/2010/02/fixing-explosions-caused-by-uitableview-scrolling.php/screen-shot-2010-02-11-at-20-31-52)

The view in the first tab loads from another nib file (MeView.xib), which contains a navigation controller.  The file’s owner has a class identify of ‘MeViewController’.  The navigation controller contains a table view, with the delegate and data source set to ‘MeTableViewController’.  It’s easier to visualise within Interface Builder:

[![Interface Builder](http://www.adoptioncurve.net/archives/2010/02/fixing-explosions-caused-by-uitableview-scrolling.php/screen-shot-2010-02-11-at-20-26-31)

The problem was that although the table view loaded fine, as soon as you tried to scroll it around the app blew up with an EXC_BAD_ACCESS (SIGBUS) error.

This had me stumped for a good half hour, until I stumbled across a cause thanks to Mr Google and a bit of poking around with the Leaks Instrument.

Basically, the problem was that as soon as the table was loaded, the delegate was being released - so as soon as I scrolled off the page, there was no cellForRowAtIndexPath method left to serve the table.  Hence the explosion.

The solution is actually quite simple - making sure that the delegate doesn’t get released.  The trick for this seems to be to add an IBOutlet declaration to the navigation controller’s class .h file, i.e MeViewController.h, and synthesize that in the corresponding .m

Then open MeView.xib in Interface Builder, and ctrl-drag from File’s Owner to the ‘Me View Table Controller’ object and set the meViewTableController outlet:

[![Screen shot 2010-02-11 at 20.25.30](http://www.adoptioncurve.net/archives/2010/02/fixing-explosions-caused-by-uitableview-scrolling.php/screen-shot-2010-02-11-at-20-25-30)

The resulting MeTableViewController connections:

[![Screen shot 2010-02-11 at 20.25.44](http://www.adoptioncurve.net/archives/2010/02/fixing-explosions-caused-by-uitableview-scrolling.php/screen-shot-2010-02-11-at-20-25-44)

This causes it to be retained, and prevents the explosion that happens if the table tries to access a delegate that isn’t there any more.  There’s also the added benefit that I stop scaring the cats by sweating at Xcode…