tableview 分区圆角(包含区头)

2022-12-01  本文已影响0人  iOS门三闫

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    CGFloat cornerRadius =10.0;

    CGRect bounds = cell.bounds;

    // 每区行数

    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];

    // 区头

    UIView *view = [self tableView:tableView viewForHeaderInSection:indexPath.section];

    //绘制曲线

    UIBezierPath *bezierPath = nil;

    if (indexPath.row == 0 && numberOfRows == 1) {

        // 一个区只有一行cell

        if (view != nil) {

            // 有区头:左下,右下为圆角

            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

        }else{

            //四个角都为圆角

            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

        }

    } else if (indexPath.row == 0) {

        // 某个区的第一行

        if (view != nil) {

            // 有区头:为矩形

            bezierPath = [UIBezierPath bezierPathWithRect:bounds];

        }else{

            //左上、右上角为圆角

            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

        }

    } else if (indexPath.row == numberOfRows - 1) {

        //某个区的最后一行:左下、右下角为圆角

        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

    } else {

        //某个区的中间行:为矩形

        bezierPath = [UIBezierPath bezierPathWithRect:bounds];

    }

   cell.backgroundColor = [UIColor clearColor];

    //新建一个layer层,设置填充色和边框颜色

    CAShapeLayer *layer = [CAShapeLayer layer];

    layer.path = bezierPath.CGPath;

    layer.fillColor = [UIColor whiteColor].CGColor;

    layer.strokeColor = [UIColor whiteColor].CGColor;

    //将layer层添加到cell.layer中,并插到最底层

    [cell.layer insertSublayer:layer atIndex:0];

}

上一篇下一篇

猜你喜欢

热点阅读