iOS如何設(shè)置tableView分組樣式cell圓角

groupTest.png

如何實現(xiàn)分組列表(group tableview)的圓角效果?如上圖:

主要實現(xiàn)思路

1.通過Core Graphics API來實現(xiàn)圖層重繪
2.實現(xiàn)UITableViewDelegate協(xié)議中的willDisplayCell方法

具體思路

  • 1.cell背景色設(shè)為透明
  • 2.新建圖層
  • 3.圓角矩形圖層繪制
  • 4.把該圖層作為自子圖層賦給UIView
  • 5.UIView賦給cell的backgroundView

核心代碼

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(tintColor)]) {
        if (tableView == self.tableView) {
            // 圓角弧度半徑
            CGFloat cornerRadius = 5.f;
            // 設(shè)置cell的背景色為透明,如果不設(shè)置這個的話,則原來的背景色不會被覆蓋
            cell.backgroundColor = UIColor.clearColor;
            
            // 創(chuàng)建一個shapeLayer
            CAShapeLayer *layer = [[CAShapeLayer alloc] init];
            CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init]; //顯示選中
            // 創(chuàng)建一個可變的圖像Path句柄,該路徑用于保存繪圖信息
            CGMutablePathRef pathRef = CGPathCreateMutable();
            // 獲取cell的size
            CGRect bounds = CGRectInset(cell.bounds, 0, 0);
            
            // CGRectGetMinY:返回對象頂點坐標
            // CGRectGetMaxY:返回對象底點坐標
            // CGRectGetMinX:返回對象左邊緣坐標
            // CGRectGetMaxX:返回對象右邊緣坐標
            
            // 這里要判斷分組列表中的第一行,每組section的第一行,每組section的中間行
            BOOL addLine = NO;
            // CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
            if (indexPath.row == 0) {
                // 初始起點為cell的左下角坐標
                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
                // 起始坐標為左下角,設(shè)為p1,(CGRectGetMinX(bounds), CGRectGetMinY(bounds))為左上角的點,設(shè)為p1(x1,y1),(CGRectGetMidX(bounds), CGRectGetMinY(bounds))為頂部中點的點,設(shè)為p2(x2,y2)。然后連接p1和p2為一條直線l1,連接初始點p到p1成一條直線l,則在兩條直線相交處繪制弧度為r的圓角。
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
                // 終點坐標為右下角坐標點,把繪圖信息都放到路徑中去,根據(jù)這些路徑就構(gòu)成了一塊區(qū)域了
                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
                addLine = YES;
            } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                // 初始起點為cell的左上角坐標
                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 {
                // 添加cell的rectangle信息到path中(不包括圓角)
                CGPathAddRect(pathRef, nil, bounds);
                addLine = YES;
            }
            // 把已經(jīng)繪制好的可變圖像路徑賦值給圖層,然后圖層根據(jù)這圖像path進行圖像渲染render
            layer.path = pathRef;
            backgroundLayer.path = pathRef;
            // 注意:但凡通過Quartz2D中帶有creat/copy/retain方法創(chuàng)建出來的值都必須要釋放
            CFRelease(pathRef);
            // 按照shape layer的path填充顏色,類似于渲染render
           // layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
            layer.fillColor = [UIColor whiteColor].CGColor;
            // 添加分隔線圖層
            if (addLine == YES) {
                CALayer *lineLayer = [[CALayer alloc] init];
                CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
                lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
                // 分隔線顏色取自于原來tableview的分隔線顏色
                lineLayer.backgroundColor = tableView.separatorColor.CGColor;
                [layer addSublayer:lineLayer];
            }
            
            // view大小與cell一致
            UIView *roundView = [[UIView alloc] initWithFrame:bounds];
            // 添加自定義圓角后的圖層到roundView中
            [roundView.layer insertSublayer:layer atIndex:0];
            roundView.backgroundColor = UIColor.clearColor;
            //cell的背景view
            //cell.selectedBackgroundView = roundView;
            cell.backgroundView = roundView;
            
             //以上方法存在缺陷當點擊cell時還是出現(xiàn)cell方形效果,因此還需要添加以下方法
            UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:bounds];
            backgroundLayer.fillColor = tableView.separatorColor.CGColor;
            [selectedBackgroundView.layer insertSublayer:backgroundLayer atIndex:0];
            selectedBackgroundView.backgroundColor = UIColor.clearColor;
            cell.selectedBackgroundView = selectedBackgroundView;
        }
    }
}

參考原文:>http://www.2cto.com/kf/201604/496884.html
下載地址:>https://github.com/dongxiexidu/tableView-

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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