Reputation: 77621
I have a UITableView
with custom cells in it.
I want to use the drawRect
method to render the cells myself (I'm using a mask to render an image with rounded corner).
Anyway, with the drawRect
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.
If I remove the drawRect
method then all the cells are visible with the labels showing.
The drawRect
method is...
- (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];
}
}
Am I doing something wrong here?
Also, if I remove the mask then it draws all the cells but I want the mask in there for the rounded corners.
EDIT - removing the image drawing
I edited the drawRect method (which is in the UITableViewCell
) (why would I put it in the tableView?)
Anyway, the new drawRect
method is...
- (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);
}
}
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.
Upvotes: 3
Views: 1194
Reputation: 170819
You're setting mask layer frame to cell's frame which is in coordinate space of cell's superview - UITableView, so for all cells but the first one actual mask frame will be outside of the cell's bounds. Try to set mask frame to cell's bounds instead:
maskLayer.frame = self.bounds;
Upvotes: 2