tableView setion cell加邊框圓角

最近項目UI需求tableViewCell 帶圓角邊框,自己寫了下像平常那樣去設置圓角邊框行不通網(wǎng)上差了一下自己又改進一下徙歼,廢話不多說先看圖在擼代碼犁河。
圖片效果如下:

51499150024_.pic副本.jpg

實現(xiàn)上面效果暫時想到兩種辦法:

一、cell重繪給section魄梯,繪制邊框桨螺。可以構建基類或者寫在category里面酿秸,方便使用代碼如下:

可直接復制代碼到項目中

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
       // 這里要判斷分組列表中的第一行灭翔,每組section的第一行,每組section的中間行

    // CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
    if (indexPath.section == 2) {
        // 圓角弧度半徑
        CGFloat cornerRadius = 3.f;
        // 設置cell的背景色為透明辣苏,如果不設置這個的話肝箱,則原來的背景色不會被覆蓋
        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);
        
        // CGRectGetMinY:返回對象頂點坐標
        // CGRectGetMaxY:返回對象底點坐標
        // CGRectGetMinX:返回對象左邊緣坐標
        // CGRectGetMaxX:返回對象右邊緣坐標
        // CGRectGetMidX: 返回對象中心點的X坐標
        // CGRectGetMidY: 返回對象中心點的Y坐標
        if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
            CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
        }else if (indexPath.row == 0) {
            // 初始起點為cell的左下角坐標
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
            // 起始坐標為左下角稀蟋,設為p煌张,(CGRectGetMinX(bounds), CGRectGetMinY(bounds))為左上角的點,設為p1(x1,y1)退客,(CGRectGetMidX(bounds), CGRectGetMinY(bounds))為頂部中點的點唱矛,設為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ù)這些路徑就構成了一塊區(qū)域了
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
//            CGPathCloseSubpath(pathRef);
            
        } 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));
//            CGPathCloseSubpath(pathRef);


        } else {
            // 添加cell的rectangle信息到path中(不包括圓角)
//            CGPathAddRect(pathRef, nil, bounds);

            //假如只要邊框
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
            CGPathMoveToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
         }
        // 把已經(jīng)繪制好的可變圖像路徑賦值給圖層窃肠,然后圖層根據(jù)這圖像path進行圖像渲染render
        layer.path = pathRef;
        backgroundLayer.path = pathRef;
        
        // 注意:但凡通過Quartz2D中帶有creat/copy/retain方法創(chuàng)建出來的值都必須要釋放
        CFRelease(pathRef);
        // 按照shape layer的path填充顏色,類似于渲染render
        layer.lineWidth = 0.5f;//線的寬度
        layer.strokeColor = [UIColor colorWithHex:@"#cccccc"].CGColor;//線的顏色
        layer.fillColor = [UIColor whiteColor].CGColor;//cellcell背景色
        // view大小與cell一致
        UIView *roundView = [[UIView alloc] initWithFrame:bounds];
        // 添加自定義圓角后的圖層到roundView中
        [roundView.layer insertSublayer:layer atIndex:0];
        roundView.backgroundColor = UIColor.clearColor;
        // cell的背景view
        cell.backgroundView = roundView;
        cell.backgroundColor = [UIColor whiteColor];
    }
   
}```
***
<p>
####二刷允、自定義一個BaseCell作為基類冤留,由它承載邊框的實現(xiàn)碧囊;然后再繼承于BaseBorderCell自定義cell進行內(nèi)容展示;
[參考](http://www.reibang.com/p/8374fdc7b180)
- BaseBorderCell.h文件

//此cell只簡單負責border的配置纤怒,cell中的內(nèi)容可以繼承于此類再進行封裝

typedef NS_ENUM(NSUInteger, BaseCellBorderStyle) {
BaseCellBorderStyleNoRound = 0,
BaseCellBorderStyleTopRound,
BaseCellBorderStyleBottomRound,
BaseCellBorderStyleAllRound,
};

@interface BaseBorderCell : UITableViewCell

@property (nonatomic, assign) BaseCellBorderStyle borderStyle;//邊框類型
@property (nonatomic, strong) UIColor *contentBorderColor;//邊框顏色
@property (nonatomic, strong) UIColor *contentBackgroundColor;//邊框內(nèi)部內(nèi)容顏色
@property (nonatomic, assign) CGFloat contentBorderWidth;//邊框的寬度糯而,這個寬度的一半會延伸到外部,如果對寬度比較敏感的要注意下
@property (nonatomic, assign) CGFloat contentMargin;//左右距離父視圖的邊距
@property (nonatomic, assign) CGSize contentCornerRadius;//邊框的圓角

  • (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath;
    //如果不想用上面的方法初始化cell泊窘,就用下面的方法設置borderStyle
  • (void)setBorderStyleWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath;
    @end
- BaseBorderCell.m文件

import "BaseBorderCell.h"

define Width self.contentView.frame.size.width

define Height self.contentView.frame.size.height

@implementation BaseBorderCell
//

  • (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
    {
    BaseBorderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
    cell = [[BaseBorderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    //一定要這里設置style熄驼,而不能在上面的判斷里面,因為cell重用的時候烘豹,只要有不同的地方都應該重新設置瓜贾,否則拿到cell的style就是上一個的樣式而自己卻沒有進行修改
    if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {
    cell.borderStyle = BaseCellBorderStyleAllRound;
    }else if (indexPath.row == 0) {
    cell.borderStyle = BaseCellBorderStyleTopRound;
    }else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {
    cell.borderStyle = BaseCellBorderStyleBottomRound;
    }else {
    cell.borderStyle = BaseCellBorderStyleNoRound;
    }
    return cell;
    }
  • (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    //配置默認值
    self.contentBorderColor = [UIColor lightGrayColor];
    self.contentBackgroundColor = [UIColor whiteColor];
    self.contentBorderWidth = 2.0;
    self.contentMargin = 10.0;
    self.contentCornerRadius = CGSizeMake(5, 5);
    }
    return self;
    }

  • (void)setBorderStyleWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
    {
    if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {
    self.borderStyle = BaseCellBorderStyleAllRound;
    }else if (indexPath.row == 0) {
    self.borderStyle = BaseCellBorderStyleTopRound;
    }else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {
    self.borderStyle = BaseCellBorderStyleBottomRound;
    }else {
    self.borderStyle = BaseCellBorderStyleNoRound;
    }
    }

  • (void)layoutSubviews
    {
    [super layoutSubviews];
    //在這里設置才能獲取到真正顯示時候的寬度,而不是原始的
    [self setupBorder];
    }

  • (void)setupBorder
    {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];

    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.lineWidth = self.contentBorderWidth;
    layer.strokeColor = self.contentBorderColor.CGColor;
    layer.fillColor = self.contentBackgroundColor.CGColor;

    UIView *view = [[UIView alloc] initWithFrame:self.contentView.bounds];
    [view.layer insertSublayer:layer atIndex:0];
    view.backgroundColor = [UIColor clearColor];
    //用自定義的view代替cell的backgroundView
    self.backgroundView = view;

    CGRect rect = CGRectMake(self.contentMargin, 0, Width - 2*self.contentMargin, Height);
    switch (self.borderStyle) {
    case BaseCellBorderStyleNoRound:
    {
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    layer.path = path.CGPath;
    }
    break;
    case BaseCellBorderStyleTopRound:
    {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:self.contentCornerRadius];
    layer.path = path.CGPath;
    }
    break;
    case BaseCellBorderStyleBottomRound:
    {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:self.contentCornerRadius];
    layer.path = path.CGPath;
    }
    break;
    case BaseCellBorderStyleAllRound:
    {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.contentCornerRadius];
    layer.path = path.CGPath;
    }
    break;
    default:
    break;
    }
    }
    @end

###個人喜歡第一種簡單高效副作用小
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末携悯,一起剝皮案震驚了整個濱河市祭芦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌憔鬼,老刑警劉巖龟劲,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異轴或,居然都是意外死亡咸灿,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門侮叮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人悼瘾,你說我怎么就攤上這事囊榜。” “怎么了亥宿?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵卸勺,是天一觀的道長。 經(jīng)常有香客問我烫扼,道長曙求,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任映企,我火速辦了婚禮悟狱,結果婚禮上,老公的妹妹穿的比我還像新娘堰氓。我一直安慰自己挤渐,他們只是感情好,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布双絮。 她就那樣靜靜地躺著浴麻,像睡著了一般得问。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上软免,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天宫纬,我揣著相機與錄音,去河邊找鬼膏萧。 笑死漓骚,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的向抢。 我是一名探鬼主播认境,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼挟鸠!你這毒婦竟也來了叉信?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤艘希,失蹤者是張志新(化名)和其女友劉穎硼身,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體覆享,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡佳遂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了撒顿。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片丑罪。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖凤壁,靈堂內(nèi)的尸體忽然破棺而出吩屹,到底是詐尸還是另有隱情,我是刑警寧澤拧抖,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布煤搜,位于F島的核電站,受9級特大地震影響唧席,放射性物質發(fā)生泄漏擦盾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一淌哟、第九天 我趴在偏房一處隱蔽的房頂上張望迹卢。 院中可真熱鬧,春花似錦徒仓、人聲如沸婶希。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽喻杈。三九已至彤枢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間筒饰,已是汗流浹背缴啡。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瓷们,地道東北人业栅。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像谬晕,于是被迫代替她去往敵國和親碘裕。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,072評論 25 707
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫攒钳、插件帮孔、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,093評論 4 62
  • iOS 設計和編程同步,上班的時候看一些設計方面的不撑,下班後一章一章的來啃文兢,編程不如吉他和繪畫抽象,理性思維還是好弄...
    kuileishi閱讀 195評論 0 1
  • 離開故鄉(xiāng)久了 思念成了一種味道 久久的彌散 填充了我思緒的空間 濃濃的苦澀 是藝術上的唯美 殘缺成了雋永 如凄美的...
    塞北清寒閱讀 333評論 4 10
  • 今天跟著姑姑感受了已婚已育創(chuàng)業(yè)女性的一天焕檬。 累成狗姆坚。 早上七點起床,收拾一會便開始叫兩個寶貝雙胞胎兒子起床实愚。從穿衣...
    蝸牛與琴閱讀 402評論 0 0