先上效果圖:

image.png
因?yàn)橛械膕ection里面有HeaderView有的沒有HeaderView,所以切圖角的時(shí)候需要判斷這種情況.
圓角直接用的layer的mask實(shí)現(xiàn)
OK,直接上代碼吧:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//圓率
CGFloat cornerRadius = 8;
//大小
CGRect bounds = cell.bounds;
//行數(shù)
NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
UIView *headerView;
if ([self respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) {
headerView=[self tableView:tableView viewForHeaderInSection:indexPath.section];
}
//繪制曲線
UIBezierPath *bezierPath = nil;
if (headerView) {
if (indexPath.row == 0 && numberOfRows == 1) {
//一個(gè)為一組時(shí),四個(gè)角都為圓角
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) 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];
}
}else{
if (indexPath.row == 0 && numberOfRows == 1) {
//一個(gè)為一組時(shí),四個(gè)角都為圓角
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
} else if (indexPath.row == 0) {
//為組的第一行時(shí),左上、右上角為圓角
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];
}
}
//新建一個(gè)圖層
CAShapeLayer *layer = [CAShapeLayer layer];
//圖層邊框路徑
layer.path = bezierPath.CGPath;
cell.layer.mask=layer;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
//圓率
CGFloat cornerRadius = 8;
//大小
CGRect bounds = view.bounds;
//繪制曲線
UIBezierPath *bezierPath = nil;
//為組的第一行時(shí),左上、右上角為圓角
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
//新建一個(gè)圖層
CAShapeLayer *layer = [CAShapeLayer layer];
//圖層邊框路徑
layer.path = bezierPath.CGPath;
view.layer.mask=layer;
}