繁体   English   中英

再次执行带有cellForRowAtIndexPath的EXEC_BAD_ACCESS

[英]EXEC_BAD_ACCESS with cellForRowAtIndexPath, again

我还有UITableView的另一个问题,从Internet加载数据后重新加载tableView后,应用程序崩溃了,崩溃发生在cellForRowAtIndexPath测量引擎中的标记位置。 我瘦了,我仍然不完全了解回收细胞的真正含义。 谢谢你的帮助

- (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;
}

看起来您正在使用会场名称,而没有在重用单元格的情况下对其进行初始化。

你有:

UILabel *venueName;

然后再:

[venueName setText:[oneVenue objectForKey:@"Name"]]; // <===CRASH!!!

在分配单元的情况下,您正在设置会场名称,但是当单元被重用时,它不会发生。 要解决此问题,您只需要:

UILabel *venueName = nil;

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

  1. 您是否尝试过构建和分析? 有时它可以帮助解决此类问题。
  2. 声明时,请尝试将venueName初始化为nil。 默认情况下,C中的临时变量未初始化,因此当您不希望使用if (!venueName)时,它可能会被绕过。

暂无
暂无

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

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