I have a UITableView and I am adding custom view to the cell. It lags really but with just 2 cells. Here’s what it looks like, when scrolling it up and down, i can see it stutters/lags so much (this is on iPhone 4s). I looked at App Store app and the scrolling with very complex tableviewcell is verrryyy smooth. Am I doing something wrong?
Here’s my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
int row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
UILabel *labelBarcode = [[UILabel alloc] initWithFrame:CGRectMake(80, 7, 150, 15)];
[labelBarcode setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
[labelBarcode setTag:10];
UILabel *labelLength = [[UILabel alloc] initWithFrame:CGRectMake(80, 22, 150, 15)];
[labelLength setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[labelLength setTag:11];
UILabel *labelThickness = [[UILabel alloc] initWithFrame:CGRectMake(80, 37, 150, 15)];
[labelThickness setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[labelThickness setTag:12];
UILabel *labelDiameter = [[UILabel alloc] initWithFrame:CGRectMake(190, 22, 100, 15)];
[labelDiameter setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[labelDiameter setTag:13];
UILabel *labelCoating = [[UILabel alloc] initWithFrame:CGRectMake(190, 37, 100, 15)];
[labelCoating setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[labelCoating setTag:14];
UIImageView *thumbView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 65, 50)];
[thumbView setContentMode:UIViewContentModeScaleToFill];
[thumbView setTag:15];
[[cell contentView] addSubview:thumbView];
[[cell contentView] addSubview:labelBarcode];
[[cell contentView] addSubview:labelLength];
[[cell contentView] addSubview:labelThickness];
[[cell contentView] addSubview:labelDiameter];
[[cell contentView] addSubview:labelCoating];
[thumbView release];
[labelBarcode release];
[labelLength release];
[labelThickness release];
[labelDiameter release];
[labelCoating release];
}
UILabel *labelBarcode = (UILabel *)[[cell contentView] viewWithTag:10];
UILabel *labelLength = (UILabel *)[[cell contentView] viewWithTag:11];
UILabel *labelThickness = (UILabel *)[[cell contentView] viewWithTag:12];
UILabel *labelDiameter = (UILabel *)[[cell contentView] viewWithTag:13];
UILabel *labelCoating = (UILabel *)[[cell contentView] viewWithTag:14];
UIImageView *thumbView = (UIImageView *)[[cell contentView] viewWithTag:15];
NSDictionary *pipe = [pipes objectAtIndex:row];
NSString *stringBarcode = [pipe objectForKey:@"Barcode"];
NSString *stringLength = [NSString stringWithFormat:@"Length: %@", [pipe objectForKey:@"Length"]];
NSString *stringThickness = [NSString stringWithFormat:@"Wall: %@",[pipe objectForKey:@"Thickness"]];
NSString *stringCoating = [NSString stringWithFormat:@"Coating: %@", [pipe objectForKey:@"Coating"]];
NSString *stringDiameter = [NSString stringWithFormat:@"Diameter: %@",[pipe objectForKey:@"Diameter"]];
[labelBarcode setText:stringBarcode];
[labelLength setText:stringLength];
[labelThickness setText:stringThickness];
[labelDiameter setText:stringDiameter];
[labelCoating setText:stringCoating];
[thumbView setImage:[UIImage imageWithData:[pipe objectForKey:@"Thumbnail"]]];
return cell;
}
The lag happens especially when I load the UITableViewController. Note that I am not allocation anything, I only allocate data (from a plist) when the app loads, and the entire app memory consumption is at 1.56 mb.
I’ve had similar problems related to having image views that are doing some type of resizing. I’d check that first. If your thumbView is doing a resize, that by itself can cause the table to lag while scrolling. If that’s the case, you could try prerendering the image resized to the correct size. I’m kinda assuming here that with only two cells you’re not getting any of your cells actually pulled out of the table. The other answers about time to create the image only really apply if you’re doing it multiple times, which would only happen when the cells are reused.