UITableView添加子视图crahes

UITableView添加子视图crahes,uitableview,subview,caanimation,Uitableview,Subview,Caanimation,我想向UITableViewCell添加包含CABasicAnimation的自定义UIView。我是这样做的 if( self.pie_chart_view == nil ) self.pie_chart_view = [[PieChartView alloc] initWithFrame:CGRectMake( 0.f, 0.f, 30.f, 30.f )]; [pie_chart_view animate_progress_from:0.0 to:1.0 with_duration

我想向UITableViewCell添加包含CABasicAnimation的自定义UIView。我是这样做的

if( self.pie_chart_view == nil )
    self.pie_chart_view = [[PieChartView alloc] initWithFrame:CGRectMake( 0.f, 0.f, 30.f, 30.f )];
[pie_chart_view animate_progress_from:0.0 to:1.0 with_duration:5.0];
[cell.contentView addSubview:pie_chart_view];
但代码崩溃,如下屏幕截图所示:

崩溃显然是由以下代码引起的

- (void)drawInContext:(CGContextRef)context {
CGRect circleRect = CGRectInset(self.bounds, 1, 1);

CGColorRef borderColor = [[UIColor whiteColor] CGColor];
CGColorRef backgroundColor = [[UIColor colorWithWhite:0 alpha: 0.75] CGColor];

CGContextSetFillColorWithColor(context, backgroundColor); <--- CRASHES ON THIS LINE WITH EXC_BAD_ACCESS
CGContextSetStrokeColorWithColor(context, borderColor);
CGContextSetLineWidth(context, 2.0f);

CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);

CGFloat radius = MIN(CGRectGetMidX(circleRect), CGRectGetMidY(circleRect));
CGPoint center = CGPointMake(radius, CGRectGetMidY(circleRect));
CGFloat startAngle = -M_PI / 2;
CGFloat endAngle = self.progress * 2 * M_PI + startAngle;
CGContextSetFillColorWithColor(context, borderColor);
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
CGContextClosePath(context);
CGContextFillPath(context);

[super drawInContext:context];
}
-(void)drawInContext:(CGContextRef)上下文{
CGRect circleRect=CGRectInset(self.bounds,1,1);
CGColorRef borderColor=[[UIColor WHITECLOR]CGColor];
CGColorRef backgroundColor=[[UIColor COLOR WITHWHITE:0 alpha:0.75]CGColor];

CGContextSetFillColorWithColor(context,backgroundColor);好的,我解决了这个问题。使用ARC时,UIColor会立即被删除。可以通过执行以下操作修复此问题:

UIColor * __autoreleasing borderColor = [UIColor whiteColor];
UIColor * __autoreleasing backgroundColor = [UIColor colorWithWhite:0 alpha: 0.75];

CGContextSetFillColorWithColor(context, backgroundColor.CGColor );
CGContextSetStrokeColorWithColor(context, borderColor.CGColor );
此后,代码不再崩溃