Reputation: 22064
I just want to say first that I've been searching for this problem, but haven't find anything similar to my problem. What I want to do, is to change the background of each cell with a image. I have three images, "top, middle and bottom" images. When I put this backgrounds the tableview gets laggy on scroll. This is how I do it:
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)];
These three images get initialized only once at view did load. Then I have the following:
- (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;
}
I have a custom cell with three different identifiers (top, middle and bottom).
This is causing the lag: [(UIImageView *)[cell viewWithTag:1] setImage:image];
. The UIImageView
is as big as the cell, to get the background effect.
Thanks!
Upvotes: 0
Views: 186
Reputation: 22064
I found the problem. It has to do with: resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 0, 10)
, it lags because it drawing the same image multiple times to stretch it. So I will have to make this images as big as the cell to get rid of the lag.
If someone find an alternative solution, please fill me in and I'll accept it if it works!
Upvotes: 1