Sheehan Alam
Sheehan Alam

Reputation: 60909

Optimizing custom UITableViewCell creation

I have the following code that just creates a custom UITableViewCell. I am creating a dynamic row height, is that expensive? Any way to optimize that?

I am also resizing the frame of one of my labels in cellForRow. Is there any way to optimize that as well?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageCell *cell = (MessageCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.bodyLabel.bounds.size.height + 30;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MessageCell";

    MessageCell *cell = (MessageCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.usernameLabel.text = [[items objectAtIndex:indexPath.row]valueForKey:@"user_login"];
    cell.bodyLabel.text = [[[items objectAtIndex:indexPath.row]valueForKey:@"body"]gtm_stringByUnescapingFromHTML];
    [Utils alignLabelWithTop:cell.bodyLabel];
    cell.dateLabel.text = [Utils toShortTimeIntervalStringFromStockTwits:[[items objectAtIndex:indexPath.row]valueForKey:@"created_at"]]; 
    [cell.avatarImageView reloadWithUrl:[[items objectAtIndex:indexPath.row]valueForKey:@"avatar_url"]];

    return cell;
}

Upvotes: 1

Views: 1933

Answers (1)

RyanR
RyanR

Reputation: 7758

  • Dynamic row height is expensive, because it cannot cache the rendered views efficiently, as the runtime doesn't know what height you're going to return for a given cell until it makes the call. Get rid of that if at all possible. I was told by Apple Engineers that it's more efficient to draw all the cells a little taller than necessary to account for a few larger rows, than to use dynamic height.
  • Cache the object returned by [items objectAtIndex:indexPath.row]
  • I don't know enough about your cell.avatarImageView, but if it isn't doing some caching of the image based on the URL, it's going to be calling out to the internet or filesystem to reload that image every time the cell is displayed. Try the EGOImageView stack, it caches it's image efficiently, and is some pretty slick code.
  • While you're in the EGO github code, grab their EGOCache and use it to cache some of the other values you have to parse, such as the bodyLabel text
  • If any of your views on that cell are transparent, watch the WWDC 2011 video on UIKit performance. They have a much more efficient method to draw transparency on tableview cells
  • Why are you changing the positioning of a label on the fly - the call to [Utils alignLabelWithTop:]?

Also watch the WWDC video on using instruments, they go over how to find where drawing code is killing performance. This year had some (some, not all) really great sessions.

Upvotes: 8

Related Questions