简体   繁体   English

UITableViewCell drawRect影响其他单元格

[英]UITableViewCell drawRect affects other cells

I have a UITableView with custom cells in it. 我有一个带有自定义单元格的UITableView

I want to use the drawRect method to render the cells myself (I'm using a mask to render an image with rounded corner). 我想使用drawRect方法自己渲染单元格(我使用蒙版来渲染带圆角的图像)。

Anyway, with the drawRect method in the tableView only draws one cell. 无论如何,tableView中的drawRect方法只绘制一个单元格。 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. 如果我删除了drawRect方法,那么所有单元格都会显示,并显示标签。

The drawRect method is... 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];
    }
}

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?) 我编辑了drawRect方法(在UITableViewCell )(为什么我会把它放在tableView中?)

Anyway, the new drawRect method is... 无论如何,新的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);
    }
}

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. 掩码似乎阻止其他单元格显示。

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. 您将掩码图层框架设置为单元格框架,该框架位于单元格超级视图的坐标空间中 - UITableView,因此对于所有单元格而言,第一个实际遮罩框架将位于单元格边界之外。 Try to set mask frame to cell's bounds instead: 尝试将掩码帧设置为单元格的边界:

maskLayer.frame = self.bounds;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM