美文网首页
tableview以section添加圆角

tableview以section添加圆角

作者: coco_CC | 来源:发表于2021-07-29 17:38 被阅读0次
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(tintColor)]) {
        if (tableView == self.dataTableview && indexPath.section == 1) {
            // 圆角弧度半径
            CGFloat cornerRadius = 10.f;
            // 设置cell的背景色为透明,如果不设置这个的话,则原来的背景色不会被覆盖
            //            cell.backgroundColor = UIColor.redColor;
            cell.contentView.backgroundColor = UIColor.orangeColor;
            
            
            // 这里要判断分组列表中的第一行,每组section的第一行,每组section的中间行
            //            BOOL addLine = NO;
            // CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
            if (indexPath.row == 0) {
                
                //这里设置的是左上和右上
                UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds   byRoundingCorners:UIRectCornerTopLeft |    UIRectCornerTopRight    cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
                CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
                maskLayer.frame = cell.bounds;
                maskLayer.path = maskPath.CGPath;
                cell.layer.mask = maskLayer;
                
            } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                
                //这里设置的是左下和左下角
                UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds   byRoundingCorners:UIRectCornerBottomLeft |    UIRectCornerBottomRight    cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
                CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
                maskLayer.frame = cell.bounds;
                maskLayer.path = maskPath.CGPath;
                cell.layer.mask = maskLayer;
                
            } else {
                
                
            }
            
            //            // view大小与cell一致
            //            UIView *roundView = [[UIView alloc] initWithFrame:bounds];
            //            // 添加自定义圆角后的图层到roundView中
            //            [roundView.layer insertSublayer:layer atIndex:0];
            //            roundView.backgroundColor = UIColor.clearColor;
            //            //cell的背景view
            //            //cell.selectedBackgroundView = roundView;
            //            cell.backgroundView = roundView;
            //
            //            //以上方法存在缺陷当点击cell时还是出现cell方形效果,因此还需要添加以下方法
            //            UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:bounds];
            //            backgroundLayer.fillColor = tableView.separatorColor.CGColor;
            //            [selectedBackgroundView.layer insertSublayer:backgroundLayer atIndex:0];
            //            selectedBackgroundView.backgroundColor = UIColor.clearColor;
            //            cell.selectedBackgroundView = selectedBackgroundView;
        }
    }
}

相关文章

网友评论

      本文标题:tableview以section添加圆角

      本文链接:https://www.haomeiwen.com/subject/eukfvltx.html