在我的应用程序中,我已经自定义了UITableViewCell并正在使用其contentView属性。 但现在问题是细胞中的数据有时会进入不同的细胞。例如:当滚动到底部或向上滚动时,单元格3中的数据将进入单元格8或9。 这是我在cellForRowAtIndexPath中使用的代码:
- (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"];
希望有所帮助。