[英]UITableViewCell drawRect affects other cells
我有一个带有自定义单元格的UITableView
。
我想使用drawRect
方法自己渲染单元格(我使用蒙版来渲染带圆角的图像)。
无论如何,tableView中的drawRect
方法只绘制一个单元格。 其余的都在那里,他们只是看不见。 我可以点击它们,但我看不到它们。
如果我删除了drawRect
方法,那么所有单元格都会显示,并显示标签。
drawRect
方法是......
- (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];
}
}
我在这里做错了吗?
此外,如果我删除掩码然后它绘制所有单元格,但我希望掩码在那里的圆角。
编辑 - 删除图像绘制
我编辑了drawRect方法(在UITableViewCell
)(为什么我会把它放在tableView中?)
无论如何,新的drawRect
方法是......
- (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);
}
}
如果图像为零,则在其位置呈现灰色矩形。 除了它只显示第一个细胞。 掩码似乎阻止其他单元格显示。
您将掩码图层框架设置为单元格框架,该框架位于单元格超级视图的坐标空间中 - UITableView,因此对于所有单元格而言,第一个实际遮罩框架将位于单元格边界之外。 尝试将掩码帧设置为单元格的边界:
maskLayer.frame = self.bounds;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.