当前位置: 代码迷 >> 综合 >> UITableView 给section整个切圆角
  详细解决方案

UITableView 给section整个切圆角

热度:25   发布时间:2024-01-26 03:43:27.0

用到UITableViewDelegate里面的tableView:willDisplayCell:forRowAtIndexPath:,该方法执行在cellForRowAtIndexPath后

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{// 圆角角度CGFloat radius = 10.f;// 设置cell 背景色为透明cell.backgroundColor = UIColor.clearColor;// 创建两个layerCAShapeLayer *normalLayer = [[CAShapeLayer alloc] init];CAShapeLayer *selectLayer = [[CAShapeLayer alloc] init];// 获取显示区域大小CGRect bounds = CGRectInset(cell.bounds, 10, 0);// 获取每组行数NSInteger rowNum = [tableView numberOfRowsInSection:indexPath.section];// 贝塞尔曲线UIBezierPath *bezierPath = nil;if (rowNum == 1) {// 一组只有一行(四个角全部为圆角)bezierPath = [UIBezierPath bezierPathWithRoundedRect:boundsbyRoundingCorners:UIRectCornerAllCornerscornerRadii:CGSizeMake(radius, radius)];} else {if (indexPath.row == 0) {// 每组第一行(添加左上和右上的圆角)bezierPath = [UIBezierPath bezierPathWithRoundedRect:boundsbyRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)cornerRadii:CGSizeMake(radius, radius)];} else if (indexPath.row == rowNum - 1) {// 每组最后一行(添加左下和右下的圆角)bezierPath = [UIBezierPath bezierPathWithRoundedRect:boundsbyRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)cornerRadii:CGSizeMake(radius, radius)];} else {// 每组不是首位的行不设置圆角bezierPath = [UIBezierPath bezierPathWithRect:bounds];}}// 把已经绘制好的贝塞尔曲线路径赋值给图层,然后图层根据path进行图像渲染rendernormalLayer.path = bezierPath.CGPath;selectLayer.path = bezierPath.CGPath;UIView *nomarBgView = [[UIView alloc] initWithFrame:bounds];// 设置填充颜色normalLayer.fillColor = [UIColor colorWithWhite:1 alpha:1.0].CGColor;// 添加图层到nomarBgView中[nomarBgView.layer insertSublayer:normalLayer atIndex:0];nomarBgView.backgroundColor = UIColor.clearColor;cell.backgroundView = nomarBgView;}
  相关解决方案