iOS - 在单元格中更改背景视图时的Laggy tableview

时间:2012-10-31 09:59:15

标签: iphone ios5 uitableview uiimageview

我只是想首先说我一直在寻找这个问题,但是没有找到与我的问题类似的东西。我想要做的是用图像改变每个单元格的背景。我有三个图像,“顶部,中部和底部”图像。当我把这个背景放在时,桌面视图在滚动时变得迟钝。我就是这样做的:

topImage = [[UIImage imageNamed:@"table_bg_top.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 0, 10)];
middleImage = [[UIImage imageNamed:@"table_bg_middle.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 0, 10)];
bottomImage = [[UIImage imageNamed:@"table_bg_bottom.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];

这三个图像只在视图加载时初始化一次。然后我有以下内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
        switch (indexPath.row) {
            case 1:
                cell = [tableView dequeueReusableCellWithIdentifier:@"TopCell"];
                [(UIImageView *)[cell viewWithTag:1] setImage:topImage];
                ((UILabel *)[cell viewWithTag:2]).text = @"First name";
                ((UILabel *)[cell viewWithTag:3]).text = firstName;
                break;
            case 2:
                cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"];
                [(UIImageView *)[cell viewWithTag:1] setImage:middleImage];
                ((UILabel *)[cell viewWithTag:2]).text = @"Last name";
                ((UILabel *)[cell viewWithTag:3]).text = lastName;
                break;
            case 3:
                cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"];
                [(UIImageView *)[cell viewWithTag:1] setImage:middleImage];
                ((UILabel *)[cell viewWithTag:2]).text = @"Birth";
                ((UILabel *)[cell viewWithTag:3]).text = [formatter stringFromDate:birth];
                break;
            case 4:
                cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"];
                [(UIImageView *)[cell viewWithTag:1] setImage:middleImage];
                ((UILabel *)[cell viewWithTag:2]).text = @"Gender";
                ((UILabel *)[cell viewWithTag:3]).text = gender;
                break;
            case 5:
                cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"];
                [(UIImageView *)[cell viewWithTag:1] setImage:middleImage];
                ((UILabel *)[cell viewWithTag:2]).text = @"Unit";
                ((UILabel *)[cell viewWithTag:3]).text = unit;
                break;
            case 6:
                cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"];
                [(UIImageView *)[cell viewWithTag:1] setImage:middleImage];
                ((UILabel *)[cell viewWithTag:2]).text = @"Height";
                if([unit isEqualToString:@"mt"])
                    ((UILabel *)[cell viewWithTag:3]).text = [NSString stringWithFormat:@"%d cm", (int)(height.floatValue*100)];
                else
                    ((UILabel *)[cell viewWithTag:3]).text = [NSString stringWithFormat:@"%d foot %d inches", (int) round((height.floatValue*100*CM_TO_INCH)) / 12, (int)round((height.floatValue*100*CM_TO_INCH)) % 12];
                break;
            case 7:
                cell = [tableView dequeueReusableCellWithIdentifier:@"BottomCell"];
                [(UIImageView *)[cell viewWithTag:1] setImage:bottomImage];
                ((UILabel *)[cell viewWithTag:2]).text = @"Weight";
                if([unit isEqualToString:@"mt"])
                    ((UILabel *)[cell viewWithTag:3]).text = [NSString stringWithFormat:@"%d kg", weight.integerValue];
                else
                    ((UILabel *)[cell viewWithTag:3]).text = [NSString stringWithFormat:@"%d pounds",(int) round((weight.floatValue*KG_TO_LBS))];
                break;
            default:
                break;
        }

    return cell;
}

我有一个带有三个不同标识符(顶部,中部和底部)的自定义单元格。 这导致滞后:[(UIImageView *)[cell viewWithTag:1] setImage:image];UIImageView与单元格一样大,以获得背景效果。

谢谢!

1 个答案:

答案 0 :(得分:1)

我发现了问题。它与:resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 0, 10)有关,它滞后是因为它多次绘制相同的图像来拉伸它。因此,我必须使这些图像与细胞一样大,以消除滞后。

如果有人找到替代解决方案,请填写我,如果有效,我会接受!