开发者

NSArray causing EXC_BAD_ACCESS

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开发者_JS百科


The problem is here:

SectionCellLabels = [NSArray arrayWithObjects:@"analysis 1",
                     @"analysis 2",
                     @"analysis 3", nil];

Your array is autoreleased, so at the end of the method it's probably not accessible anymore.

To fix that, simply append a retain message like this:

SectionCellLabels = [[NSArray arrayWithObjects:..., nil] retain];

And be sure to release the array in another place, like your dealloc method.

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.


Try this

SectionCellLabels = [[NSArray arrayWithObjects:@"analysis 1",
                     @"analysis 2",
                     @"analysis 3", nil] retain];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜