UITableView Crashes When Scrolling

Discussion in 'iOS Programming' started by KE5DXX, Nov 15, 2010.

  1. KE5DXX macrumors newbie

    Jun 20, 2010
    Lubbock, TX
    I am writing an iPhone application that utilizes a UITableView within a UINavigationController on the front screen. I started a new project in Xcode using the Navigation Controller template, and added the following code to my RootViewController.h file:

    #import <UIKit/UIKit.h>
    @interface RootViewController : UITableViewController {
    	NSArray *navOptions;
    And this code to my RootViewController.m UITableView dataSource methods:

    // Customize the number of rows in the table view.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	navOptions = [NSArray arrayWithObjects:@"The|Ridge News", @"Small Groups", @"Trips", @"Merchandise", @"Events", @"Staff", nil];
        return [navOptions count];
    // 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];
    	[[cell textLabel] setText:[NSString stringWithFormat:@"%@", [navOptions objectAtIndex:indexPath.row]]];
        return cell;
    However, when I scroll the tableView up, it crashes my application. I get an EXC_BAD_ACCESS error on the line:

    	[[cell textLabel] setText:[NSString stringWithFormat:@"%@", [navOptions objectAtIndex:indexPath.row]]];
    It seems to me that the program crashes because it's looking for an object in the array that doesn't exist. I set my numberOfRowsInSection method and so I keep trying to figure out why the tableView is still trying to load data past that number.

    This is almost identical to the format used by the book I'm following, and I've tried a few remedies. I've set a break point on RootViewController.m's dealloc method to make sure it wasn't being deallocated, and I've repeatedly checked variable values and memory allocation to try and diagnose the problem. I keep coming up with what I stated above, I just don't know how to fix it. Any help is greatly appreciated, and any constructive criticism as well. Thanks!
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
  3. AbhishekApple, Nov 15, 2010
    Last edited: Nov 19, 2010

    AbhishekApple macrumors member

    Aug 5, 2010
    U should initialize the array so that it stays in memory at least until the view is unload... Tableview doesn't loads all the data at once, it loads only the rows which are visible on screen...so whn u scroll it loads the data from the bound source (in ur case navOptions)...

    And as u haven't allocated memory to navOptions it releases the navOption memory (automatically)

    Go wid chowns suggestion read the MM document...
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Actually, your "correct" code doesn't allocate it either. ;)
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    There is a very simple method that allows the code to keep using arrayWithObjects:, yet retain a claim of ownership on the array, so it won't disappear with its autorelease pool. It doesn't involve using alloc or initWithObjects:.
  6. Comrade Yeti macrumors newbie

    Nov 3, 2010
    You need to throw a retain on navOptions. It is set to autorelease and will dealloc once your program hits the next event loop.
    navOptions = [[NSArray arrayWithObjects:/*blah blah*/, nil] [B]retain[/B]]
    (and remember to release it in your dealloc)

    Reread the Memory Management docs. All the "convienence" methods return autoreleased objects that WILL be dealloc'd when the next event loop runs. If you want them to stick around*, they need to be retained.

    Once you've read the Memory Management docs, read them again. This mistake is one of the most common.

    *In general, if you want ANYTHING to stick around, you should be retaining it. If you don't, there is no guarantee it will exist when you need it.

    **[edit] and move the creation of navOptions OUT of your numberOfRowsInSection... research the [[alloc] init] pattern, and use it.

Share This Page