Unable to draw in UITableViewCell drawRect

I'm having trouble drawing the drawRect method of my custom UITableViewCell. Here is the code I'm 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 debugged to make sure that all numerical values ​​made sense, and it seemed like they were doing it. I can’t understand why nothing is drawn on the screen.

For what it's worth, it's a cell defined in nib. And I am building with iOS 7 sdk.

Any ideas?

tahnks

+5
source share
4 answers

You should probably not do this in your UITableViewCell's own drawRect. Instead, create a custom UIView and add it as a preview.

See also this answer .

+9
source

Try setting the backgroungcolor property to transparent.

 self.backgroundColor = [UIColor clearColor] 
+15
source

You must install

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

Try setting the backgroundColor contentView property to clear the color

 self.contentView = [UIColor clearColor]; 
-2
source

Source: https://habr.com/ru/post/958603/


All Articles