美文网首页
UITableView分组切圆角方法

UITableView分组切圆角方法

作者: ZouJgg | 来源:发表于2021-11-23 13:42 被阅读0次

效果图:

IMG_36CD19E21795-1.jpeg

看代码更清晰

#import "ViewController1.h"

@interface DCBViewController1 ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@end

@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:UIColor.dcb_Level02BackgroundColor];

    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.view);
        make.left.equalTo(self.view).offset(12);
        make.right.equalTo(self.view).offset(-12);
    }];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setBackgroundColor:UIColor.dcb_Level02BackgroundColor];
        _tableView.showsVerticalScrollIndicator = NO;
        /// 分割线的颜色
        [_tableView setSeparatorColor:[UIColor dcb_Level02BackgroundColor]];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 50)];
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
    
    cell.backgroundColor = UIColor.clearColor;
    cell.contentView.backgroundColor = UIColor.whiteColor;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    ///分割线顶满
    cell.separatorInset = UIEdgeInsetsZero;
    cell.layoutMargins = UIEdgeInsetsZero;
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 12.f;
}

/// 切分组的核心方法 注意cell的contentView背景需要设置颜色;如果为whiteColor则需要将cell.backgroundColor设置为clearColor
- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
    ///每个分组的行数
    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];

    ///圆角大小
    CGSize cornerRadii = CGSizeMake(8, 8);
    ///切圆角位置
    UIRectCorner corners = UIRectCornerAllCorners;
    
    if (indexPath.row == 0 && numberOfRows == 1) {
        ///一个为一组时,四个角都为圆角
        corners = UIRectCornerAllCorners;

    } else if (indexPath.row == 0) {
        ///分组第一行,左上、右上角为圆角
        corners = UIRectCornerTopLeft | UIRectCornerTopRight;

    } else if (indexPath.row + 1 == numberOfRows) {
        ///分组最后一行,左下、右下角为圆角
        corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;

    } else {
        ///中间的都为矩形
        cornerRadii = CGSizeZero;
    }
    
    CGRect cellRect = cell.contentView.bounds;
    UIBezierPath*maskPath = [UIBezierPath bezierPathWithRoundedRect:cellRect byRoundingCorners:corners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = cellRect;
    maskLayer.path = maskPath.CGPath;
    cell.contentView.layer.mask = maskLayer;
}

@end

经过渲染性能检测验证UIBezierPath+ CAShapeLayer方法会导致离屏渲染的问题
找到了其他不会离屏渲染的方法:

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
   ///每个分组的行数
    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];

    CGFloat cornerRadius = 8.f;
    cell.backgroundColor = UIColor.clearColor;
     
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init];
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGRect bounds = cell.bounds;
              
    if (indexPath.row == 0 && numberOfRows == 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));
         
    } else if (indexPath.row + 1 == numberOfRows) {
        ///分组最后一行,左下、右下角为圆角
        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);
    }
     
    layer.path = pathRef;
    backgroundLayer.path = pathRef;
    CFRelease(pathRef);
    layer.fillColor = [UIColor redColor].CGColor;

    UIView *roundView = [[UIView alloc] initWithFrame:bounds];
    [roundView.layer insertSublayer:layer atIndex:0];
    roundView.backgroundColor = UIColor.clearColor;
    cell.backgroundView = roundView;

    UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
    backgroundLayer.fillColor = [UIColor clearColor].CGColor;
    [selectedBackgroundView.layer insertSublayer:backgroundLayer below:cell.layer];
    selectedBackgroundView.backgroundColor = UIColor.clearColor;
    cell.selectedBackgroundView = selectedBackgroundView;
}

相关文章

网友评论

      本文标题:UITableView分组切圆角方法

      本文链接:https://www.haomeiwen.com/subject/rnjwtrtx.html