iOS開發(fā)之UITableviewCell實現(xiàn)兩個圓角

tableView時常有一個效果憾股,就是當cell在section的第一個位置或者最后一行時鹿蜀,第一個cell左上角和右上角顯示圓角箕慧,最后一個cell左下角和右下角顯示圓角。方法一才用方法一可以實現(xiàn)圓角效果茴恰,但是如果table設置了滑動刪除的話颠焦,第一行和最后一行滑動時候看不到刪除按鈕如下面代碼創(chuàng)建一個cell的子類 CornerTableViewCell.h

#importtypedef NS_ENUM(NSInteger,RoundCornerType)

{

KKRoundCornerCellTypeTop,

KKRoundCornerCellTypeBottom,

KKRoundCornerCellTypeSingleRow,

KKRoundCornerCellTypeDefault

};

@interface CornerTableViewCell : UITableViewCell

{

CGFloat _cornerRadius;

}

@property(nonatomic,readwrite,assign)RoundCornerType roundCornerType;

-(id)initWithCornerRadius:(CGFloat)radius Style:(UITableViewCellStyle)tyle reuseIdentifier:(NSString*)identifier;

@end

CornerTableViewCell.m

#import "CornerTableViewCell.h"

@implementation CornerTableViewCell

-(id)initWithCornerRadius:(CGFloat)radius Style:(UITableViewCellStyle)tyle reuseIdentifier:(NSString*)identifier

{

self = [super initWithStyle:tyle reuseIdentifier:identifier];

if (self) {

_cornerRadius = radius;

}

return self;

}

-(void)setFrame:(CGRect)frame

{

[super setFrame:frame];

UIBezierPath *path;

switch (_roundCornerType) {

case KKRoundCornerCellTypeTop: {

path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];

break;

}

case KKRoundCornerCellTypeBottom: {

path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];

break;

}

case KKRoundCornerCellTypeSingleRow: {

path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];

break;

}

case KKRoundCornerCellTypeDefault:

default: {

self.layer.mask = nil;

return;

}

}

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.frame = self.bounds;

maskLayer.path = path.CGPath;

self.layer.mask = maskLayer;

}

@end

實現(xiàn)table的dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{

return 10;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *kCellID = @"CELLID";

CornerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];

if (!cell) {

cell = [[CornerTableViewCell alloc] initWithCornerRadius:10.0f Style:UITableViewCellStyleDefault reuseIdentifier:kCellID];

}

if ([tableView numberOfRowsInSection:indexPath.section] == 1) {

cell.roundCornerType = KKRoundCornerCellTypeSingleRow;

} else if (indexPath.row == 0) {

cell.roundCornerType = KKRoundCornerCellTypeTop;

} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {

cell.roundCornerType = KKRoundCornerCellTypeBottom;

} else {

cell.roundCornerType = KKRoundCornerCellTypeDefault;

}

cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 256.0

green:arc4random() % 256 / 256.0

blue:arc4random() % 256 / 256.0

alpha:1.0];

cell.textLabel.text = [NSString stringWithFormat:@"第%ld組第%ld行", indexPath.section + 1, indexPath.row + 1];

return cell;

}

方法二

方法二實現(xiàn)圓角沒有方法一所說的問題,方法二通過給backGroudView添加mask來實現(xiàn)圓角效果

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

if ([cell respondsToSelector:@selector(tintColor)]) {

if (tableView == self.tableView) {

CGFloat cornerRadius = 5.f;

cell.backgroundColor = UIColor.clearColor;

CAShapeLayer *layer = [[CAShapeLayer alloc] init];

CGMutablePathRef pathRef = CGPathCreateMutable();

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));

} else {

CGPathAddRect(pathRef, nil, bounds);

addLine = YES;

}

layer.path = pathRef;

CFRelease(pathRef);

layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;

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];

}

UIView *testView = [[UIView alloc] initWithFrame:bounds];

[testView.layer insertSublayer:layer atIndex:0];

testView.backgroundColor = UIColor.clearColor;

cell.backgroundView = testView;

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末往枣,一起剝皮案震驚了整個濱河市伐庭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌分冈,老刑警劉巖圾另,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異雕沉,居然都是意外死亡集乔,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門坡椒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來扰路,“玉大人,你說我怎么就攤上這事倔叼∮姿ィ” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵缀雳,是天一觀的道長。 經(jīng)常有香客問我梢睛,道長肥印,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任绝葡,我火速辦了婚禮深碱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘藏畅。我一直安慰自己敷硅,他們只是感情好,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布愉阎。 她就那樣靜靜地躺著绞蹦,像睡著了一般。 火紅的嫁衣襯著肌膚如雪榜旦。 梳的紋絲不亂的頭發(fā)上幽七,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音溅呢,去河邊找鬼澡屡。 笑死猿挚,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的驶鹉。 我是一名探鬼主播绩蜻,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼室埋!你這毒婦竟也來了办绝?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤词顾,失蹤者是張志新(化名)和其女友劉穎八秃,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肉盹,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡昔驱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了上忍。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片骤肛。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖窍蓝,靈堂內(nèi)的尸體忽然破棺而出腋颠,到底是詐尸還是另有隱情,我是刑警寧澤吓笙,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布淑玫,位于F島的核電站,受9級特大地震影響面睛,放射性物質(zhì)發(fā)生泄漏絮蒿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一叁鉴、第九天 我趴在偏房一處隱蔽的房頂上張望土涝。 院中可真熱鬧,春花似錦幌墓、人聲如沸但壮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蜡饵。三九已至,卻和暖如春袭祟,著一層夾襖步出監(jiān)牢的瞬間验残,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留您没,地道東北人鸟召。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像氨鹏,于是被迫代替她去往敵國和親欧募。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

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