Can't draw in UITableViewCell's drawRect

依然范特西╮ 提交于 2019-12-09 11:50:15

问题


I'm having trouble drawing in the drawRect method of my custom UITableViewCell. Here is the code I am using

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx  = UIGraphicsGetCurrentContext();
    CGPoint origin    = _faceView.frame.origin;
    CGFloat width     = _faceView.frame.size.width;
    CGFloat height    = _faceView.frame.size.height;
    CGFloat border    = 2.0f;


    CGPoint startPt   = CGPointMake(origin.x + width/2, self.frame.size.height);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, startPt.x, startPt.y);

    CGPoint basePt    = CGPointMake(startPt.x, origin.y - height - border);
    CGContextAddLineToPoint(ctx, basePt.x, basePt.y);

    CGRect circleRect = CGRectMake(origin.x - border, origin.y - border, width + 2 * border, height + 2 * border);
    CGContextAddEllipseInRect(ctx, circleRect);

    UIColor *color = [UIColor redColor];
    CGContextSetFillColorWithColor(ctx, color.CGColor);
    CGContextSetStrokeColorWithColor(ctx, color.CGColor);
    CGContextSetLineWidth(ctx, 1.0f);

    CGContextDrawPath(ctx, kCGPathFillStroke);
}

I've debugged to make sure that all of the numeric values make sense and it appears that they do. Can't really find out why nothing is being drawn on screen.

For what its worth, this is a cell defined in a nib as well. And i'm building with the iOS 7 sdk.

Any ideas?

tahnks


回答1:


You probably shouldn't do this in UITableViewCell's own drawRect. Instead, create a custom UIView and add it as a subview.

See also this answer.




回答2:


Try set backgroungcolor property to transparent color

self.backgroundColor = [UIColor clearColor]



回答3:


You have to set

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
       .....
       [cell setNeedsDisplay];
       return cell;
}



回答4:


Try set backgroundColor of contentView property to clear color

self.contentView = [UIColor clearColor];


来源:https://stackoverflow.com/questions/18883975/cant-draw-in-uitableviewcells-drawrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!