简体   繁体   中英

Removing a custom sublayer from a custom uitableviewcell

I need rounded, variable-height tableview cells.

I use the following code to create the rounded background:

- (void)roundView:(UIView *)view withRadius:(float)radius andColour:(UIColor *)colour
{
    view.backgroundColor = [UIColor whiteColor];
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGRect bounds = CGRectInset(view.bounds, 0.0f, 0.0f);
    CGPathAddRoundedRect(pathRef, nil, bounds, radius, radius);
    layer.path = pathRef;
    CFRelease(pathRef);
    layer.fillColor = colour.CGColor;
    [view.layer insertSublayer:layer atIndex:0];
}  

The problem is that if a tall cell is rounded, when it is reused, the earlier sublayer is still there and although the new (lesser) height is correct, the appearance is that the bottom edge of the box is not rounded. Presumably, the (larger) pre-existing layer is being clipped.

An obvious thought was to remove the sublayer, but I can't a way of doing it. I've tried creating a new cell, without reusing one, but this doesn't seem possible.

在单元格类中创建一个自定义图层属性,将其分配给该-removeFromSuperLayer ,然后在单元格的-prepareForReuse对其调用-prepareForReuse

I recalled somewhere that I'd done this before to a button...

self.buttonCancel.layer.cornerRadius = 10;
self.buttonCancel.clipsToBounds = YES;

and it works here too...

cell.textBlurb.layer.cornerRadius = 10;
cell.textBlurb.clipsToBounds = YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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