views:

34

answers:

3

Hi everyone, 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 dequeueReusableCellWithIdentifier: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;
}
A: 

I'd hazard a guess that your self.venueList contains pointers that are invalid, and so crashes when dereferenced on the line you marked.

jbm
venueList of okay for sure, i've checked it by enumeration
Mapedd
A: 
  1. Have you tried a Build and Analyze? It can sometimes help with things like this.
  2. Try initializing venueName to nil when you declare it. Temporary variables in C are not initialized by default, so your if (!venueName) might be getting bypassed when you don't want it to be.
Frank Schmitt
+2  A: 

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;
kharrison
The key is that the check if ( ! venueName) will not work, because it's an uninitalized value that is non-zero.
Kendall Helmstetter Gelner