//设置cell圆角
- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
// 圆角角度
CGFloat radius = 6.f;
// 设置cell 背景色为透明
cell.backgroundColor = UIColor.clearColor;
// 创建两个layer
CAShapeLayer *normalLayer = [[CAShapeLayer alloc] init];
CAShapeLayer *selectLayer = [[CAShapeLayer alloc] init];
// 获取显示区域大小
CGRect bounds = CGRectInset(cell.bounds, 6, 0);
// 获取每组行数
NSInteger rowNum = [tableView numberOfRowsInSection:indexPath.section];
// 贝塞尔曲线
UIBezierPath *bezierPath = nil;
if (rowNum == 1) {
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
}else{
if (indexPath.row == 0) {
// 每组第一行(添加左上和右上的圆角)
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(radius, radius)];
}else if (indexPath.row == rowNum - 1){
// 每组最后一行(添加左下和右下的圆角)
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(radius, radius)];
}else{
// 每组不是首位的行不设置圆角
bezierPath = [UIBezierPath bezierPathWithRect:bounds];
}
}
// 把已经绘制好的贝塞尔曲线路径赋值给图层,然后图层根据path进行图像渲染render
normalLayer.path = bezierPath.CGPath;
selectLayer.path = bezierPath.CGPath;
UIView *nomarBgView = [[UIView alloc] initWithFrame:bounds];
// 设置填充颜色
normalLayer.fillColor = [[UIColor whiteColor] CGColor];
// 添加图层到nomarBgView中
[nomarBgView.layer insertSublayer:normalLayer atIndex:0];
nomarBgView.backgroundColor = UIColor.clearColor;
// nomarBgView.backgroundColor = UIColor.whiteColor;
cell.backgroundView = nomarBgView;
//此时圆角显示就完成了,但是如果没有取消cell的点击效果,还是会出现一个灰色的长方形的形状,再用上面创建的selectLayer给cell添加一个selectedBackgroundView
UIView *selectBgView = [[UIView alloc] initWithFrame:bounds];
selectLayer.fillColor = [[UIColor whiteColor] CGColor];
[selectBgView.layer insertSublayer:selectLayer atIndex:0];
selectBgView.backgroundColor = UIColor.clearColor;
cell.selectedBackgroundView = selectBgView;
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}
设置TableView Section圆角
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 自定义UITableViewCell 上面的控件设置圆角 自定义cell的drawRect 方法,并在该方法里面设...
- tableView圆角边框解决方案 iOS 7之前,图下圆角边框很容易设置iOS 7之后,tableviewcel...
- TableView的Section切圆角在实际的项目开发中是比较常见的,出现频率最高是在个人中心页面。大体的效果如...
- 一行代码给section添加圆角(Swift) 在UITableViewDelegate的willDisplayC...