我只想说,我一直在寻找这个问题,但没有找到与我的问题类似的东西。我想做的是用图像更改每个单元格的背景。我有三个图像,“顶部、中间和底部”图像。当我放置这个背景时,表格视图在滚动时变得迟钝。我就是这样做的:
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
一样大,以获得背景效果。
谢谢!