Better Performance for setting backgroundView: willDisplayCell or init?
I am just curious, what has better performance for setting a custom backgroundView of a UITableViewCell?
Option 0) Subclass of UITableViewCell init method
@implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andReleases:(NSArray*)releases {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tablecell.png"]] autorelease];
    }
    return self;
}
Option 1) willDisplayCell delegate method
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  开发者_JAVA技巧  cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tablecell.png"]] autorelease];
}
The option 0 is better in this case since you set the background once when you create the cell, and willDisplayCell will set it each time you display a cell. And since you'll reuse UITableViewCells you'll create cell less time then you'll display them.
But avoid premature optimization, optimize only when you feel the performance isn't good enough.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论