EXEC_BAD_ACCESS with cellForRowAtIndexPath, again
i've another problem with the UITableView, app crashes after reloading tableView after loading data from internet, the crash happens in marked place in cellForRowAtIndexPath methood. I thinnk i still don't fully understand what actually mean recycling cells. Thanks for any help
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UILabel *venueName;
UIImageView *logo;
UITableViewCell *cell = [tableView dequeueReusableCellWi开发者_Python百科thIdentifier:CellIdentifier];
if (cell == nil)
{
NSLog(@">>> GIT 1<<<");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
venueName = [[UILabel alloc] initWithFrame:CGRectZero];
[venueName setLineBreakMode:UILineBreakModeWordWrap];
[venueName setMinimumFontSize:FONT_SIZE];
[venueName setNumberOfLines:0];
[venueName setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[venueName setTag:1];
venueName.backgroundColor = [UIColor clearColor];
[[cell contentView] addSubview:venueName];
logo = [[UIImageView alloc] initWithFrame:CGRectZero];
[logo setTag:10];
[[cell contentView] addSubview:logo];
cell.backgroundView = [[[UIImageView alloc] init] autorelease];//new
}
NSMutableDictionary *oneVenue ;
if ([self.venueList count] > 0) {
oneVenue = [self.venueList objectAtIndex:indexPath.row];
if (!venueName) {
venueName = (UILabel*)[cell viewWithTag:1];
}
[venueName setText:[oneVenue objectForKey:@"Name"]]; // <===CRASH!!!
[venueName setFrame:CGRectMake(CELL_CONTENT_MARGIN,CELL_VENUE_LEVEL ,80 , 30)];
[logo setImage:[UIImage imageNamed:@"event.png"]];
[logo setFrame:CGRectMake(10, 5, 76, 60)];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImage *rowBackground;
rowBackground = [UIImage imageNamed:@"evbgd_yell.png"];
((UIImageView *)cell.backgroundView).image = rowBackground;
}
return cell;
}
looks like you are using venueName without initialising it in situations where you are reusing a cell.
You have:
UILabel *venueName;
and then later:
[venueName setText:[oneVenue objectForKey:@"Name"]]; // <===CRASH!!!
in situations where you allocate a cell you are setting venueName but when a cell is reused it does not happen. To fix you should just need:
UILabel *venueName = nil;
I'd hazard a guess that your self.venueList
contains pointers that are invalid, and so crashes when dereferenced on the line you marked.
- Have you tried a Build and Analyze? It can sometimes help with things like this.
- Try initializing
venueName
to nil when you declare it. Temporary variables in C are not initialized by default, so yourif (!venueName)
might be getting bypassed when you don't want it to be.
精彩评论