问题
I am subclassing UIView and using instances of that to set my UITableViewCell backgroundView and selectedBackedView properties. I am receiving an EXC_BAD_ACCESS error in the drawRect method of my UIView subclass.
if(nil == cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.backgroundView = [[CCViewBackground alloc]init];
cell.selectedBackgroundView = [[CCViewBackground alloc]init];
}
UIView subclass CCBackgroundView -drawRect:
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef redColor =
[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0].CGColor;
CGContextSetFillColorWithColor(context, redColor); //Receiving EXC_BAD_ACCESS here
CGContextFillRect(context, self.bounds);
}
回答1:
I assume you're using ARC. If so, you are running into a well-known problem where the CGColorRef
is released earlier than you expect. This article explains the problem in detail and provides several solutions.
来源:https://stackoverflow.com/questions/12237554/cgcontextref-exc-bad-access-error