开发者

Better Performance for setting backgroundView: willDisplayCell or init?

开发者 https://www.devze.com 2023-03-12 18:46 出处:网络
I am just curious, what has better performance for setting a custom backgroundView of a UITableViewCell?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消