为你的tableView的自定义cell加上圆角

来源:互联网 发布:java程序员面试问题 编辑:程序博客网 时间:2024/06/01 07:58

有的时候自己自定义了一个cell,这个cell是撑满全屏的,但是有些时候我们想要这个cell距左边10 据右边10的宽度处显示圆角,而不是整个cell

那你就需要tableView的代理方法:

#pragma mark - #pragma mark 设置圆角- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    if ([cell respondsToSelector:@selector(tintColor)]) {        if (tableView == self.tableView) {            CGFloat cornerRadius = 5.0f;            cell.backgroundColor = UIColor.clearColor;            CAShapeLayer *layer = [[CAShapeLayer alloc] init];            CGMutablePathRef pathRef = CGPathCreateMutable();            CGRect bounds = CGRectInset(cell.bounds, 5, 0);            BOOL addLine = NO;            if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {                CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);            } else if (indexPath.row == 0) {                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));                addLine = YES;            } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));            } else {                CGPathAddRect(pathRef, nil, bounds);                addLine = YES;                            }            layer.path = pathRef;            CFRelease(pathRef);            layer.fillColor = [UIColor clearColor].CGColor;                        if (addLine == YES) {                CALayer *lineLayer = [[CALayer alloc] init];                CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);                lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);                lineLayer.backgroundColor = tableView.separatorColor.CGColor;                [layer addSublayer:lineLayer];            }            UIView *testView = [[UIView alloc] initWithFrame:bounds];            [testView.layer insertSublayer:layer atIndex:0];            testView.backgroundColor = UIColor.clearColor;            cell.backgroundView = testView;        }    }}


0 0
原创粉丝点击