UITableView对每一组(section)设置圆角

- (void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath{

    //圆率

    CGFloatcornerRadius =8.0;

    //大小

    CGRectbounds = cell.bounds;

    //行数

    NSIntegernumberOfRows = [tableViewnumberOfRowsInSection:indexPath.section];


    //绘制曲线

    UIBezierPath*bezierPath =nil;


    if(indexPath.row==0&& numberOfRows ==1) {

        //一个为一组时,四个角都为圆角

        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

    }elseif(indexPath.row==0) {

        //为组的第一行时,左上、右上角为圆角

        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

    }elseif(indexPath.row== numberOfRows -1) {

        //为组的最后一行,左下、右下角为圆角

        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

    }else{

        //中间的都为矩形

        bezierPath = [UIBezierPathbezierPathWithRect:bounds];

    }

    //cell的背景色透明

    cell.backgroundColor = [UIColor clearColor];

    //新建一个图层

    CAShapeLayer *layer = [CAShapeLayer layer];

    //图层边框路径

    layer.path= bezierPath.CGPath;

    //图层填充色,也就是cell的底色

    layer.fillColor = [UIColor whiteColor].CGColor;

    //图层边框线条颜色

    /*

     如果self.tableView.style = UITableViewStyleGrouped时,每一组的首尾都会有一根分割线,目前我还没找到去掉每组首尾分割线,保留cell分割线的办法。

     所以这里取巧,用带颜色的图层边框替代分割线。

     这里为了美观,最好设为和tableView的底色一致。

     设为透明,好像不起作用。

     */

    layer.strokeColor = [UIColor whiteColor].CGColor;

    //将图层添加到cell的图层中,并插到最底层

    [cell.layer insertSublayer:layer atIndex:0];


}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容