UITableViewCell内容视图 – 添加UILabels

我目前有我的UITableView我的cellForRowAtIndexPath下面的代码:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* CellIdentifier = @"Cell"; UILabel* nameLabel = nil; UILabel* valueLabel = nil; UILabel *percentLabel = nil; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if ( cell == nil ) { inthere = YES; cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake( 7.0, 0.0, 140.0, 44.0 )] autorelease]; nameLabel.tag = 21; nameLabel.font = [UIFont systemFontOfSize: 12.0]; nameLabel.textAlignment = UITextAlignmentLeft; nameLabel.textColor = [UIColor darkGrayColor]; nameLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; nameLabel.backgroundColor = [UIColor clearColor]; [cell.contentView addSubview: nameLabel]; valueLabel = [[[UILabel alloc] initWithFrame: CGRectMake( 165.0, 0.0, 80, 44.0 )] autorelease]; valueLabel.tag = 22; valueLabel.font = [UIFont systemFontOfSize: 11]; valueLabel.textAlignment = UITextAlignmentRight; valueLabel.textColor = [UIColor blueColor]; valueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; valueLabel.backgroundColor = [UIColor clearColor]; [cell.contentView addSubview: valueLabel]; percentLabel = [[[UILabel alloc] initWithFrame: CGRectMake(245, 0.0, 65, 44.0 )] autorelease]; percentLabel.tag = 24; percentLabel.font = [UIFont systemFontOfSize: 12]; percentLabel.textAlignment = UITextAlignmentRight; percentLabel.textColor = [UIColor blueColor]; percentLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; percentLabel.backgroundColor = [UIColor clearColor]; [cell.contentView addSubview: percentLabel]; } else { nameLabel = (UILabel*)[cell.contentView viewWithTag:21]; valueLabel = (UILabel*)[cell.contentView viewWithTag:22]; percentLabel = (UILabel *)[cell.contentView viewWithTag:24]; } ...and then I initialize the text of each of these three labels... 

}

但是我想要做的是根据细胞的不同,这三个标签是不同的颜色。 例如,第3节中的所有单元格都需要具有红色名称标签,而第5节中的所有单元格都需要具有绿色值标签。 但是,如果我在初始化所有标签的文本后插入代码:

 if(indexPath.section==3) { nameLabel.textColor = [UIColor redColor]; } if(indexPath.section==5) { valueLabel.textColor = [UIColor greenColor]; } 

然后一切都搞砸了,桌子都是毛茸茸的,文字在奇怪的地方,标签是错误的颜色,有点随意看。

我如何为我的表格中的每个单元格指定这三个标签的颜色?

每次单元被回收时,必须重置标签的textColor

尝试这个,

 if (indexPath.section == 3) { nameLabel.textColor = [UIColor redColor]; nameLabel.frame = CGRectMake(someX, someY, someWidth, someHeight); valueLabel.textColor = [UIColor blackColor]; valueLabel.frame = CGRectMake(someX, someY, someWidth, someHeight); } if (indexPath.section == 5) { nameLabel.textColor = [UIColor blackColor]; nameLabel.frame = CGRectMake(someX, someY, someWidth, someHeight); valueLabel.textColor = [UIColor greenColor]; valueLabel.frame = CGRectMake(someX, someY, someWidth, someHeight); } 

你的代码似乎没问题。 我可以build议尝试的唯一的事情是不使用标签的autorelease ,而是添加到contentView后立即release

问题发生了,因为UITableViewCells被系统随机回收 。 这就是为什么你需要设置单元默认值:

 if(indexPath.section==3) nameLabel.textColor = [UIColor redColor]; else nameLabel.textColor = [UIColor blackColor]; 

顺便说一句你的单元格看起来如此复杂,我build议使用Interface Builder来创build它。