I have a problem with using an NSArray to populate a UITableView. I’m sure I’m doing something daft but I can’t figure it out. When I try and do a simple count I get the EXC_BAD_ACCESS, which I know is because I am trying to read from a memory location that doesn’t exist.
My .h file has this:
@interface AnalysisViewController : UITableViewController
{
StatsData *statsData;
NSArray *SectionCellLabels;
}
My .m has this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"AnalysisViewController:viewWillAppear");
// Step 1 - Create the labels array
SectionCellLabels = [NSArray arrayWithObjects:@"analysis 1",
@"analysis 2",
@"analysis 3", nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"AnalysisViewController:cellForRowAtIndexPath");
// Check for reusable cell first, use that if it exists
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"UITableViewCell"];
// If there is no reusable cell of this type, create a new one
if (!cell) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"] autorelease];
}
/******* The line of code below causes the EXC_BAD_ACCESS error *********/
NSLog(@"%d",[SectionCellLabels count]);
return cell;
}
Any help greatly appreciated.
Mike
The problem is here:
Your array is autoreleased, so at the end of the method it’s probably not accessible anymore.
To fix that, simply append a
retainmessage like this:And be sure to
releasethe array in another place, like yourdeallocmethod.One extra tip, you may want to use names with the first character in lowercase, so they don’t appear to be classes. You can even notice that this confused StackOverflow’s highlighting.