简体   繁体   English

再次执行带有cellForRowAtIndexPath的EXEC_BAD_ACCESS

[英]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. 我还有UITableView的另一个问题,从Internet加载数据后重新加载tableView后,应用程序崩溃了,崩溃发生在cellForRowAtIndexPath测量引擎中的标记位置。 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;
}

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;

我可能会猜测您的self.venueList包含无效的指针,因此在标记的行上取消引用时会崩溃。

  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. 声明时,请尝试将venueName初始化为nil。 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. 默认情况下,C中的临时变量未初始化,因此当您不希望使用if (!venueName)时,它可能会被绕过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM