UITableView 設(shè)置section圓角

有兩種方法削茁,第二種方法效果更加圓滑
tableView的背景顏色設(shè)置為透明

_tableView.backgroundColor = [UIColor clearColor];

在tableView所在的控制器中添加

第一種方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    //圓率
    CGFloat cornerRadius = 10.0;
    //大小
    CGRect bounds = cell.bounds;
    //行數(shù)
    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];

    //繪制曲線
    UIBezierPath *bezierPath = nil;

    if (indexPath.row == 0 && numberOfRows == 1) {
       //一個為一組時,四個角都為圓角
       bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    } else if (indexPath.row == 0) {
       //為組的第一行時,左上、右上角為圓角
       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的背景色透明
    cell.backgroundColor = [UIColor clearColor];
    //新建一個圖層
    CAShapeLayer *layer = [CAShapeLayer layer];
    //圖層邊框路徑
    layer.path = bezierPath.CGPath;
    //圖層填充色,也就是cell的底色
    layer.fillColor = [UIColor whiteColor].CGColor;
    //圖層邊框線條顏色
    /*
    如果self.tableView.style = UITableViewStyleGrouped時,每一組的首尾都會有一根分割線,目前我還沒找到去掉每組首尾分割線,保留cell分割線的辦法饭入。
    所以這里取巧,用帶顏色的圖層邊框替代分割線。
    這里為了美觀,最好設(shè)為和tableView的底色一致悉盆。
    設(shè)為透明,好像不起作用争便。
    */
    layer.strokeColor = [UIColor whiteColor].CGColor;
    //將圖層添加到cell的圖層中,并插到最底層
    [cell.layer insertSublayer:layer atIndex:0];
}

第二種方法效果更加圓滑

/** 設(shè)置分區(qū)圓角 */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 圓角弧度半徑
    CGFloat cornerRadius = 10.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
    // 第一個參數(shù),是整個 cell 的 bounds, 第二個參數(shù)是距左右兩端的距離,第三個參數(shù)是距上下兩端的距離
    CGRect bounds = CGRectInset(cell.bounds, 10, 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));
//        addLine = YES;
    } else {
        CGPathAddRect(pathRef, nil, bounds);
        addLine = YES;
    }
    
    // 把已經(jīng)繪制好的可變圖像路徑賦值給圖層,然后圖層根據(jù)這圖像path進(jìn)行圖像渲染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;
    
    // view大小與cell一致
    UIView *roundView = [[UIView alloc] initWithFrame:bounds];
    // 添加自定義圓角后的圖層到roundView中
    [roundView.layer insertSublayer:layer atIndex:0];
    roundView.backgroundColor = UIColor.clearColor;
    // cell的背景view
    cell.backgroundView = roundView;
    
    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];
    }
    
    // 以上方法存在缺陷當(dāng)點(diǎn)擊cell時還是出現(xiàn)cell方形效果盛龄,因此還需要添加以下方法
    // 如果你 cell 已經(jīng)取消選中狀態(tài)的話,那以下方法是不需要的.
    //    UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:bounds];
    //    backgroundLayer.fillColor = [UIColor colorWithRed:0.90 green:0.90 blue:0.90 alpha:1].CGColor;
    //    [selectedBackgroundView.layer insertSublayer:backgroundLayer atIndex:0];
    //    selectedBackgroundView.backgroundColor = UIColor.clearColor;
    //    cell.selectedBackgroundView = selectedBackgroundView;
    //    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市芳誓,隨后出現(xiàn)的幾起案子余舶,更是在濱河造成了極大的恐慌,老刑警劉巖锹淌,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匿值,死亡現(xiàn)場離奇詭異,居然都是意外死亡赂摆,警方通過查閱死者的電腦和手機(jī)挟憔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門钟些,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人绊谭,你說我怎么就攤上這事政恍。” “怎么了达传?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵篙耗,是天一觀的道長。 經(jīng)常有香客問我宪赶,道長宗弯,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任搂妻,我火速辦了婚禮蒙保,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘叽讳。我一直安慰自己追他,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布邑狸。 她就那樣靜靜地躺著,像睡著了一般涤妒。 火紅的嫁衣襯著肌膚如雪单雾。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天民褂,我揣著相機(jī)與錄音茄菊,去河邊找鬼。 笑死赊堪,一個胖子當(dāng)著我的面吹牛面殖,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播哭廉,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼脊僚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了遵绰?” 一聲冷哼從身側(cè)響起辽幌,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤增淹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后舶衬,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體埠通,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡赎离,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年逛犹,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片梁剔。...
    茶點(diǎn)故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡虽画,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出荣病,到底是詐尸還是另有隱情码撰,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布个盆,位于F島的核電站脖岛,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏颊亮。R本人自食惡果不足惜柴梆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望终惑。 院中可真熱鬧绍在,春花似錦、人聲如沸雹有。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽霸奕。三九已至溜宽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間质帅,已是汗流浹背适揉。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留临梗,地道東北人涡扼。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像盟庞,于是被迫代替她去往敵國和親吃沪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容