表行泄漏

时间:2010-09-01 10:37:45

标签: iphone

我在iphone表视图中有以下行,它们有时会将文本从其中一行泄漏到另一行。

if (row <1)
    {
        cell.detailTextLabel.text=@"h3ello";
             UIImage *image = [UIImage imageNamed:@"icon.png"];
           cell.imageView.image = image;   
    }
      else if (row ==2)
    {
        cell.detailTextLabel.text=@"world";
        UIImage *image = [UIImage imageNamed:@"ticon.png"];
        cell.imageView.image = image
}

有时这些图像会被搞砸,我还有另一行包含文字颜色。当我滚动时,这会导致各种随机行获得红色。我不确定错误是什么..

修改

这是涉及的罪魁祸首

 cell.textLabel.textColor = [UIColor redColor];

一旦我删除这一行,没有任何行与颜色混淆。 如何将这种颜色实现到特定的行而不会随机泄漏到其他行上。

2 个答案:

答案 0 :(得分:2)

因为默认情况下单元格是可重复使用的,所以应始终为您使用过的所有单元格设置所有参数。 如果要更改一个单元格的颜色,还应为OTHER单元格设置默认颜色。 因此,在每种情况下使用cell.textLabel.textColor = ....并使用正确的颜色。

答案 1 :(得分:1)

Skie给出的理由是正确的。让我再澄清一下:

cell.textLabel.text = @"";
cell.textLabel.textColor = [UIColor clearColor];
cell.imageView.image = nil;

if (row <1)
{
    cell.detailTextLabel.text=@"h3ello";
    UIImage *image = [UIImage imageNamed:@"icon.png"];
    cell.imageView.image = image;   
}
  else if (row ==2)
{
    cell.detailTextLabel.text=@"world";
    UIImage *image = [UIImage imageNamed:@"ticon.png"];
    cell.imageView.image = image
} else {
}