Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

KE5DXX

macrumors newbie
Original poster
Jun 20, 2010
21
0
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:

Code:
#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
	
	NSArray *navOptions;
	
}

@end

And this code to my RootViewController.m UITableView dataSource methods:

Code:
// 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:

Code:
	[[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!
 
Code:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	navOptions = [COLOR="Red"][NSArray arrayWithObjects:[/COLOR]@"The|Ridge News", @"Small Groups", @"Trips", @"Merchandise", @"Events", @"Staff", nil];

    return [navOptions count];
}
[/QUOTE]
The red-hilited code creates an array that you don't own.  You then assign this array to the navOptions ivar, and continue to use it as if you did own it.

You should review the memory management and object ownership rules:
[url]http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html[/url]

[QUOTE]This is almost identical to the format used by the book I'm following, and I've tried a few remedies. 
[/QUOTE]
"Almost identical" doesn't count in programming.  Tiny differences can be very significant.  What book are you learning from?  Be specific.  What remedies did you try?  Be specific.
 
navOptions = [NSArray arrayWithObjects:mad:"The|Ridge News", @"Small Groups", @"Trips", @"Merchandise", @"Events", @"Staff", nil];

correct one //edited
navOptions = [[NSArray alloc] initWithObjects:@"The|Ridge News", @"Small Groups", @"Trips", @"Merchandise", @"Events", @"Staff", nil];

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...
 
Last edited:
And as u haven't allocated memory to navOptions it releases the navOption memory (automatically)

Go wid chowns suggestion read the MM document...

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:.
 
You need to throw a retain on navOptions. It is set to autorelease and will dealloc once your program hits the next event loop.
Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.