簡體   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