I have a table view with hundreds of custom drawn UITableViewCells which are drawn using the drawRect: method. These cells all share the same color styles. So, in each drawRect:, I do something like this:
-(void)drawRect:(CGRect)rect
{
UIColor *bgColor = [UIColor colorWithWhite:0.9 alpha:1.0];
UIColor *textColor = [UIColor colorWithWhite:0.98 alpha:1.0];
UIColor *strokeColor = [UIColor colorWithWhite:0.6 alpha:1.0];
/* use colors */
}
And so as you’re scrolling down the table, this gets called hundreds of times. I’m not really sure if what I’m doing is efficient though. Is there a better way to create these colors, maybe with some reusability? Should I cache these colors in some global variable? Is what I am doing expensive or negligible?
First you should profile with Instruments to see if a considerable amount of time is spent in these calls. If this is the case, then make them static variables and initialize them on first use: