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([cellrespondsToSelector:@selector(tintColor)]) {

if(tableView ==self.tableView) {

CGFloatcornerRadius =5.f;

cell.backgroundColor=UIColor.clearColor;

CAShapeLayer*layer = [[CAShapeLayeralloc]init];

CGMutablePathRefpathRef =CGPathCreateMutable();

CGRectbounds =CGRectInset(cell.bounds,10,0);

BOOLaddLine =NO;

if(indexPath.row==0&& indexPath.row== [tableViewnumberOfRowsInSection:indexPath.section]-1) {

CGPathAddRoundedRect(pathRef,nil, bounds, cornerRadius, cornerRadius);

}elseif(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;

}elseif(indexPath.row== [tableViewnumberOfRowsInSection: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= [UIColorcolorWithWhite:1.falpha:0.8f].CGColor;

if(addLine ==YES) {

CALayer*lineLayer = [[CALayeralloc]init];

CGFloatlineHeight = (1.f/ [UIScreenmainScreen].scale);

lineLayer.frame=CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);

lineLayer.backgroundColor= tableView.separatorColor.CGColor;

[layeraddSublayer:lineLayer];

}

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

[testView.layerinsertSublayer:layeratIndex:0];

testView.backgroundColor=UIColor.clearColor;

cell.backgroundView= testView;

} } }

下載地址:>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)容