在我的应用程序中,我定制了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;
}
看到我更新的答案,应该解决这个问题。 –