我有uitableviewcontroller。在cellForRowAtIndexPath方法中,我为标签添加了自定义设置:

UILabel *lblMainLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 9, 150, 25)];
    lblMainLabel.text = c.Name;
    lblMainLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    lblMainLabel.backgroundColor = [UIColor clearColor];
    lblMainLabel.textColor = [UIColor whiteColor];
    [cell.contentView addSubview:lblMainLabel];
    [lblMainLabel release];

但当我在表中向上或向下滚动时,它总是将这个标签添加到我之前遗漏的内容之上?

最佳答案

当创建单元格时,应该创建一次UIababel.
您的代码应该如下所示:

if (cell == nil) {
   cell = ...
   UILabel *lblMainLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 9, 150, 25)];
   lblMainLabel.tag = 42;
   lblMainLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
   lblMainLabel.backgroundColor = [UIColor clearColor];
   lblMainLabel.textColor = [UIColor whiteColor];
   [cell.contentView addSubview:lblMainLabel];
   [lblMainLabel release];
}
UILabel *lblMainLabel = [cell viewWithTag:42];
lblMainLabel.text = c.Name;

10-07 20:05