UITableView Section增加圓角

先上效果圖:


image.png

因?yàn)橛械膕ection里面有HeaderView有的沒有HeaderView,所以切圖角的時(shí)候需要判斷這種情況.
圓角直接用的layer的mask實(shí)現(xiàn)
OK,直接上代碼吧:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    //圓率
    CGFloat cornerRadius = 8;
    //大小
    CGRect bounds = cell.bounds;
    //行數(shù)
    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
    UIView *headerView;
    if ([self respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) {
        headerView=[self tableView:tableView viewForHeaderInSection:indexPath.section];
    }
    
    //繪制曲線
    UIBezierPath *bezierPath = nil;
    if (headerView) {
        if (indexPath.row == 0 && numberOfRows == 1) {
            //一個(gè)為一組時(shí),四個(gè)角都為圓角
            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) 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];
        }
    }else{
        if (indexPath.row == 0 && numberOfRows == 1) {
            //一個(gè)為一組時(shí),四個(gè)角都為圓角
            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        } else if (indexPath.row == 0) {
            //為組的第一行時(shí),左上、右上角為圓角
            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];
        }
    }
    
    //新建一個(gè)圖層
    CAShapeLayer *layer = [CAShapeLayer layer];
    //圖層邊框路徑
    layer.path = bezierPath.CGPath;
    cell.layer.mask=layer;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    //圓率
    CGFloat cornerRadius = 8;
    //大小
    CGRect bounds = view.bounds;
    
    //繪制曲線
    UIBezierPath *bezierPath = nil;
    //為組的第一行時(shí),左上、右上角為圓角
    bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    //新建一個(gè)圖層
    CAShapeLayer *layer = [CAShapeLayer layer];
    //圖層邊框路徑
    layer.path = bezierPath.CGPath;
    view.layer.mask=layer;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容