2011-07-21 32 views
0

在我的应用程序中,我定制了UITableViewCell并使用其contentView属性。 但现在问题是细胞中的数据有时会在不同的细胞中。例如:当滚动到底部或向上滚动时,单元格3中的数据将进入单元格8或9。 这里使用的cellForRowAtIndexPath它的IAM代码:为什么UITableViewCell在单个单元中更改它们的值?

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 
    CGFloat fontSize = [UIFont systemFontSize]; 
    CGFloat smallFontSize = [UIFont smallSystemFontSize]; 
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease]; 
     mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5,0,150,42)] autorelease]; 
     mainLabel.font = [UIFont systemFontOfSize:smallFontSize]; 
     mainLabel.numberOfLines = 3; 
     mainLabel.lineBreakMode = UILineBreakModeWordWrap; 
     mainLabel.backgroundColor = [UIColor clearColor]; 
     mainLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:mainLabel]; 

     secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(160,0,160,42)] autorelease]; 
     secondLabel.font = [UIFont systemFontOfSize:smallFontSize]; 
     secondLabel.numberOfLines = 3; 
     secondLabel.lineBreakMode = UILineBreakModeWordWrap; 
     secondLabel.backgroundColor = [UIColor clearColor]; 
     secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:secondLabel]; 


    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.textLabel.font = [UIFont systemFontOfSize:smallFontSize]; 
    cell.detailTextLabel.font = [UIFont systemFontOfSize:smallFontSize]; 
    NSArray * titles = [[NSArray alloc] initWithObjects:@"Project ID", @"Status", @"Approval Date", @"Closing Date", @"Country",@"Region Name", @"Env", @"Team Full Name",@"Borrower", @"Impagency", @"Lending Cost", @"IBRDPlus", nil]; 

    switch (indexPath.section) { 
     case 0: 

      mainLabel.text = [titles objectAtIndex:indexPath.row]; 
      mainLabel.adjustsFontSizeToFitWidth = YES; 
      mainLabel.numberOfLines = 4; 
      mainLabel.lineBreakMode = UILineBreakModeWordWrap; 

      secondLabel.text = [self.basic objectAtIndex:indexPath.row]; 
      secondLabel.adjustsFontSizeToFitWidth = YES; 
      secondLabel.numberOfLines = 4; 
      secondLabel.lineBreakMode = UILineBreakModeWordWrap; 
      break; 
     case 1: 

      mainLabel.text = [[self.allSectors objectAtIndex:indexPath.row] valueForKey:@"SECTORNAME"]; 
      mainLabel.adjustsFontSizeToFitWidth = YES; 
      mainLabel.numberOfLines = 4; 
      mainLabel.lineBreakMode = UILineBreakModeWordWrap; 

      secondLabel.text = [[self.allSectors objectAtIndex:indexPath.row] valueForKey:@"SECTORPCT"]; 
      secondLabel.adjustsFontSizeToFitWidth = YES; 
      secondLabel.numberOfLines = 4; 
      secondLabel.lineBreakMode = UILineBreakModeWordWrap; 

      break; 
     case 2: 

      mainLabel.text = [[self.themes objectAtIndex:indexPath.row] valueForKey:@"THEME_NAME"]; 
      mainLabel.adjustsFontSizeToFitWidth = YES; 
      mainLabel.numberOfLines = 4; 
      mainLabel.lineBreakMode = UILineBreakModeWordWrap; 

      secondLabel.text = [[self.themes objectAtIndex:indexPath.row] valueForKey:@"THEMEPCT"]; 
      secondLabel.adjustsFontSizeToFitWidth = YES; 
      secondLabel.numberOfLines = 4; 
      secondLabel.lineBreakMode = UILineBreakModeWordWrap; 

      break; 

     default: 
      break; 
    } 

    return cell; 
} 
+0

看到我更新的答案,应该解决这个问题。 –

回答

1

这是因为该行(你需要有反正)的发生:

UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier]; 

基本上是因为细胞被重用的内容撑在单元格中,因此您需要在dequeu后清理该单元格,然后设置新的contentView。

什么是发生在这里明确的是,即使你重置的secondLabel的文本,当你在这里设置您的变量:

secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(160,0,160,42)] autorelease]; 

已创建的最后一个单元格是什么secondLabel指向所有时间,所以你真的总是改变相同的标签,因此我会建议这样的事情:

UILabel *lblCell; 
for (id label in [cell.contentView subviews]) { 
      if ([label isKindOfClass:[UILabel class]]) { 
       lblCell = label; 
      } 
     } 

lblCell.text = [[self.themes objectAtIndex:indexPath.row] valueForKey:@"THEMEPCT"]; 

希望有所帮助。

+0

我完全不明白这一点。如果您可以请详细说明我需要使用此循环的位置以及我需要替换lblCell的位置。 – Ashutosh

+0

@Ashutosh循环只是为了获得一个指向被复制的单元格contentView标签的指针,所以你可以修改它,每当你改变标签中的文本时你都需要这样做。 –

+0

我仍然没有得到这个地方。我很新,这是我第一次自定义单元格。所以这将是很好的,你可以在我的代码中编辑,这将帮助我正确理解它。 – Ashutosh

相关问题