有兩種方法削茁,第二種方法效果更加圓滑
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;
// }
}