In my drawing routine for a custom UITableViewCell, was using the following without any problems:
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor* backgroundColor = [UIColor colorForHex:@"FFFEFF"];
[backgroundColor set];
CGContextFillRect(context, rect);
Of course, this is quite standard except for my interface on the UIColor object for colorForHex which takes a string and breaks this down into RGB values of 0-255 and returns a UIColor object with the following (a is defaulted to 1.0f):
return [UIColor colorWithRed:((float) r/255.0f) green:((float) g/255.0f) blue:((float) b/255.0f) alpha:a];
Now this works fine, but I tried to improve this and centralise the the colour (and font) creation to try to speed up the draw routine as much as possible. This is where I get the problem.
I create a protocol using:
@protocol ItemListTableViewCellDelegate
-(UIFont*) fontForId:(int) fontID;
-(UIColor*) colorForId:(int) colorID;
@end
The controller implements this protocol and implements the colorForId method as:
-(UIColor*) colorForId:(int) colorID {
UIColor* requestedColor = nil;
switch (colorID) {
case BACKGROUND_COLOR_ID:
{
requestedColor = backgroundColor;
}
break;
}
return requestedColor;
}
where backgroundColor is created as a member of the controller as a UIColor* in the initWithStyle method of the controller. This is being called and the backgroundColor is being created.
The cell has an NSObject<ItemListTableViewCellDelegate>* cellDelegate
member which is initialised with the controller so that the cell can call
UIColor* backgroundColor = [cellDelegate colorForId:BACKGROUND_COLOR_ID];
which appears to return the correct pointer to the UIColor in the controller, but when 'set' is called on this colour in the cell draw method then the cell throws a BAD ACCESS error and I can't work out why.
Any pointers in the right direction?