为UITableView分组添加圆角

之前有人问我这个问题,如何为tableview的每一个分组添加一个圆角。就像下图的卡片效果,其实很简单。我们只需要把每一组的第一个cell和最后一个cell切掉两个角就好了。开始前请将cell的背景色设置透明,contentView的背景色设置为你想要的颜色。代码在下面。。。

-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath

{

if(indexPath.section!=1&& indexPath.row==0) {

        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.contentView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];

        //创建 layer

        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

        maskLayer.frame= cell.contentView.bounds;

        //赋值

        maskLayer.path= maskPath.CGPath;

        cell.contentView.layer.mask= maskLayer;

    }

    if((indexPath.section==0&& indexPath.row==5)||(indexPath.section==1&& indexPath.row==0)||(indexPath.section==2&& indexPath.row==2)){


        UIBezierPath*maskPath;

//由于我的第二组只有一个cell ,所以这里分开设置。将第二组四个角全切

        if(indexPath.row==0) {

            maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.contentView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];

        }else{

            maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.contentView.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];

        }

        //创建 layer

        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

        maskLayer.frame= cell.contentView.bounds;

        //赋值

        maskLayer.path= maskPath.CGPath;

        cell.contentView.layer.mask= maskLayer;


    }

   }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容