iOS UITableView圓角Cell

開發(fā)中有遇到要把長方形的cell改為圓角的,網(wǎng)上查了資料發(fā)現(xiàn)好多網(wǎng)友在如下方法中重繪cell帘睦。具體代碼大家可以自行百度喉磁。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{}
然后自己思考有沒有別的方法可以實(shí)現(xiàn)。于是就嘗試了一下官脓。自己重寫了如下方法协怒。發(fā)現(xiàn)效果還不錯(cuò)。
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{}
如圖:

廢話不多說直接上代碼

.h中聲明方法

#import <UIKit/UIKit.h>

@interface HanTableViewCell : UITableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellForRowAtIndexPath:(NSIndexPath *)indexPath withTabel:(UITableView *)tableView;
@end

.m 中實(shí)現(xiàn)

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellForRowAtIndexPath:(NSIndexPath *)indexPath withTabel:(UITableView *)tableView
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
        bgView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:bgView];
        UIBezierPath *path = [UIBezierPath bezierPath];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
            maskLayer.frame = bgView.bounds;
            bgView.layer.mask = maskLayer;
            maskLayer.fillColor = [UIColor whiteColor].CGColor;
        if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
        {
            path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds cornerRadius:22];
        }
        else if (indexPath.row == 0)
        {
            path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectEdgeLeft|UIRectEdgeTop cornerRadii:CGSizeMake(22, 22)];
        }
        else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
        {
            path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectEdgeRight|UIRectEdgeBottom cornerRadii:CGSizeMake(22, 22)];
        }
        else
        {
            path = [UIBezierPath bezierPathWithRect:bgView.bounds];
        }
        maskLayer.path = path.CGPath;
        [bgView.layer addSublayer:maskLayer];
        if (indexPath.row != [tableView numberOfRowsInSection:indexPath.section]-1  )
        {
            CALayer *lineLayer = [CALayer layer];
            lineLayer.borderWidth = 1;
            lineLayer.borderColor = tableView.separatorColor.CGColor;
            lineLayer.frame = CGRectMake(20, 43, tableView.frame.size.width - 40, 1);
            [bgView.layer addSublayer:lineLayer];
        }
    }
    return self;
}

具體使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    HanTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[HanTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellStr cellForRowAtIndexPath:indexPath withTabel:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType  = UITableViewCellAccessoryNone;
    }
    if (indexPath.row == 0)
    {
        cell.imageView.image = [UIImage imageNamed:@"l.jpeg"];
    }
    cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
    return cell;
}
重寫的方法多傳了兩個(gè)參數(shù):tableView卑笨、indexPath 用來區(qū)分自定義的cell孕暇。
注:由于自定義了cell的分割線,需要把自帶的分割線去掉赤兴。
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
至此完工妖滔。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市桶良,隨后出現(xiàn)的幾起案子座舍,更是在濱河造成了極大的恐慌,老刑警劉巖陨帆,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件曲秉,死亡現(xiàn)場離奇詭異采蚀,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)承二,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門榆鼠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人亥鸠,你說我怎么就攤上這事妆够。” “怎么了负蚊?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵神妹,是天一觀的道長。 經(jīng)常有香客問我家妆,道長鸵荠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任揩徊,我火速辦了婚禮腰鬼,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘塑荒。我一直安慰自己熄赡,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布齿税。 她就那樣靜靜地躺著彼硫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪凌箕。 梳的紋絲不亂的頭發(fā)上拧篮,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天,我揣著相機(jī)與錄音牵舱,去河邊找鬼串绩。 笑死,一個(gè)胖子當(dāng)著我的面吹牛芜壁,可吹牛的內(nèi)容都是我干的礁凡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼慧妄,長吁一口氣:“原來是場噩夢啊……” “哼顷牌!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起塞淹,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤窟蓝,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后饱普,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體运挫,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡状共,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了滑臊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片口芍。...
    茶點(diǎn)故事閱讀 39,926評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡箍铲,死狀恐怖雇卷,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情颠猴,我是刑警寧澤关划,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站翘瓮,受9級(jí)特大地震影響贮折,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜资盅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一调榄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧呵扛,春花似錦每庆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蓝晒,卻和暖如春腮出,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背芝薇。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工胚嘲, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人洛二。 一個(gè)月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓馋劈,卻偏偏與公主長得像,于是被迫代替她去往敵國和親灭红。 傳聞我的和親對象是個(gè)殘疾皇子侣滩,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評論 2 354

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