Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableViewCell drawRect affects other cells
    text
    copied!<p>I have a <code>UITableView</code> with custom cells in it.</p> <p>I want to use the <code>drawRect</code> method to render the cells myself (I'm using a mask to render an image with rounded corner).</p> <p>Anyway, with the <code>drawRect</code> method in the tableView only draws one cell. All the rest are there they are just invisible. I can tap them but I can't see them.</p> <p>If I remove the <code>drawRect</code> method then all the cells are visible with the labels showing.</p> <p>The <code>drawRect</code> method is...</p> <pre><code>- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGRect mask = CGRectMake(10, 10, rect.size.width - 20, rect.size.height - 20); UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:mask cornerRadius:4]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = self.frame; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; if (self.image == nil) { CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.6 alpha:1.0].CGColor); CGContextFillRect(context, rect); } else { float width = rect.size.width - 20; float height = self.image.size.height / self.image.size.width * width; CGRect imageRect = CGRectMake((rect.size.width - width) * 0.5, (rect.size.height - height) * 0.5, width, height); [self.image drawInRect:imageRect]; } } </code></pre> <p>Am I doing something wrong here?</p> <p>Also, if I remove the mask then it draws all the cells but I want the mask in there for the rounded corners.</p> <p><strong>EDIT - removing the image drawing</strong></p> <p>I edited the drawRect method (which is in the <code>UITableViewCell</code>) (why would I put it in the tableView?)</p> <p>Anyway, the new <code>drawRect</code> method is...</p> <pre><code>- (void)drawRect:(CGRect)rect { if (self.image == nil) { CGContextRef context = UIGraphicsGetCurrentContext(); CGRect mask = CGRectMake(10, 10, rect.size.width - 20, rect.size.height - 20); UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:mask cornerRadius:4]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = self.frame; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.7 alpha:1.0].CGColor); CGContextFillRect(context, rect); } } </code></pre> <p>If the image is nil then it renders a grey rectangle in its place. Except it only shows the first cell. The mask seems to stop the other cells from displaying.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload