1、修改 UITableViewCell 的圓角,只需要在自定義的 cell 中重寫 cell 的 frame 和設置 layer 的cornerRadius和masksToBounds兩個屬性。
即:
1 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ 2 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 3 if (self) { 4 self.layer.cornerRadius = 5; 5 self.layer.masksToBounds = YES; 6 7 self.selectionStyle = UITableViewCellSelectionStyleNone; 8 } 9 10 return self; 11 } 12 13 14 - (void)setFrame:(CGRect)frame{ 15 16 CGRect f = frame; 17 f.origin.x = 10; 18 f.size.width = frame.size.width - 20; 19 [super setFrame:f]; 20 }
2、設置 UITableView 中 section 的圓角
1 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3 if ([cell respondsToSelector:@selector(tintColor)]) { 4 if (tableView == self.tableView) { 5 // 圓角弧度半徑 6 CGFloat cornerRadius = 5.f; 7 // 設置cell的背景色為透明,如果不設置這個的話,則原來的背景色不會被覆蓋 8 cell.backgroundColor = UIColor.clearColor; 9 10 // 創建一個shapeLayer 11 CAShapeLayer *layer = [[CAShapeLayer alloc] init]; 12 CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init]; //顯示選中 13 // 創建一個可變的圖像Path句柄,該路徑用於保存繪圖信息 14 CGMutablePathRef pathRef = CGPathCreateMutable(); 15 // 獲取cell的size 16 CGRect bounds = CGRectInset(cell.bounds, 0, 0); 17 18 // CGRectGetMinY:返回對象頂點坐標 19 // CGRectGetMaxY:返回對象底點坐標 20 // CGRectGetMinX:返回對象左邊緣坐標 21 // CGRectGetMaxX:返回對象右邊緣坐標 22 23 // 這里要判斷分組列表中的第一行,每組section的第一行,每組section的中間行 24 BOOL addLine = NO; 25 // CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius); 26 if (indexPath.row == 0) { 27 // 初始起點為cell的左下角坐標 28 CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds)); 29 // 起始坐標為左下角,設為p1,(CGRectGetMinX(bounds), CGRectGetMinY(bounds))為左上角的點,設為p1(x1,y1),(CGRectGetMidX(bounds), CGRectGetMinY(bounds))為頂部中點的點,設為p2(x2,y2)。然后連接p1和p2為一條直線l1,連接初始點p到p1成一條直線l,則在兩條直線相交處繪制弧度為r的圓角。 30 CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius); 31 CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); 32 // 終點坐標為右下角坐標點,把繪圖信息都放到路徑中去,根據這些路徑就構成了一塊區域了 33 CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds)); 34 addLine = YES; 35 } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { 36 // 初始起點為cell的左上角坐標 37 CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds)); 38 CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius); 39 CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); 40 // 添加一條直線,終點坐標為右下角坐標點並放到路徑中去 41 CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds)); 42 } else { 43 // 添加cell的rectangle信息到path中(不包括圓角) 44 CGPathAddRect(pathRef, nil, bounds); 45 addLine = YES; 46 } 47 // 把已經繪制好的可變圖像路徑賦值給圖層,然后圖層根據這圖像path進行圖像渲染render 48 layer.path = pathRef; 49 backgroundLayer.path = pathRef; 50 // 注意:但凡通過Quartz2D中帶有creat/copy/retain方法創建出來的值都必須要釋放 51 CFRelease(pathRef); 52 // 按照shape layer的path填充顏色,類似於渲染render 53 // layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor; 54 layer.fillColor = [UIColor whiteColor].CGColor; 55 // 添加分隔線圖層 56 if (addLine == YES) { 57 CALayer *lineLayer = [[CALayer alloc] init]; 58 CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale); 59 lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight); 60 // 分隔線顏色取自於原來tableview的分隔線顏色 61 lineLayer.backgroundColor = tableView.separatorColor.CGColor; 62 [layer addSublayer:lineLayer]; 63 } 64 65 // view大小與cell一致 66 UIView *roundView = [[UIView alloc] initWithFrame:bounds]; 67 // 添加自定義圓角后的圖層到roundView中 68 [roundView.layer insertSublayer:layer atIndex:0]; 69 roundView.backgroundColor = UIColor.clearColor; 70 //cell的背景view 71 //cell.selectedBackgroundView = roundView; 72 cell.backgroundView = roundView; 73 74 //以上方法存在缺陷當點擊cell時還是出現cell方形效果,因此還需要添加以下方法 75 UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:bounds]; 76 backgroundLayer.fillColor = tableView.separatorColor.CGColor; 77 [selectedBackgroundView.layer insertSublayer:backgroundLayer atIndex:0]; 78 selectedBackgroundView.backgroundColor = UIColor.clearColor; 79 cell.selectedBackgroundView = selectedBackgroundView; 80 } 81 } 82 }