美文网首页iOS 开发 学习
iOS如何设置tableView分组样式cell圆角

iOS如何设置tableView分组样式cell圆角

作者: 清蘂翅膀的技术 | 来源:发表于2017-05-16 14:27 被阅读87次

    groupTest.png

    如何实现分组列表(group tableview)的圆角效果?如上图:

    主要实现思路

    1.通过Core Graphics API来实现图层重绘

    2.实现UITableViewDelegate协议中的willDisplayCell方法

    具体思路

    1.cell背景色设为透明

    2.新建图层

    3.圆角矩形图层绘制

    4.把该图层作为自子图层赋给UIView

    5.UIView赋给cell的backgroundView

    核心代码

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

    {

    if([cellrespondsToSelector:@selector

    (tintColor)]) {

    if(tableView ==self.tableView

    ) {

    CGFloatcornerRadius =5.f

    ;

    cell.

    backgroundColor=UIColor.clearColor

    ;

    CAShapeLayer*layer = [[CAShapeLayeralloc]init

    ];

    CGMutablePathRefpathRef =CGPathCreateMutable

    ();

    CGRectbounds =CGRectInset(cell.bounds,10,0

    );

    BOOLaddLine =NO

    ;

    if(indexPath.row==0&& indexPath.row== [tableViewnumberOfRowsInSection:indexPath.section]-1

    ) {

    CGPathAddRoundedRect(pathRef,nil

    , bounds, cornerRadius, cornerRadius);

    }

    elseif(indexPath.row==0

    ) {

    CGPathMoveToPoint(pathRef,nil,CGRectGetMinX(bounds),CGRectGetMaxY

    (bounds));

    CGPathAddArcToPoint(pathRef,nil,CGRectGetMinX(bounds),CGRectGetMinY(bounds),CGRectGetMidX(bounds),CGRectGetMinY

    (bounds), cornerRadius);

    CGPathAddArcToPoint(pathRef,nil,CGRectGetMaxX(bounds),CGRectGetMinY(bounds),CGRectGetMaxX(bounds),CGRectGetMidY

    (bounds), cornerRadius);

    CGPathAddLineToPoint(pathRef,nil,CGRectGetMaxX(bounds),CGRectGetMaxY

    (bounds));

    addLine =

    YES

    ;

    }

    elseif(indexPath.row== [tableViewnumberOfRowsInSection:indexPath.section]-1

    ) {

    CGPathMoveToPoint(pathRef,nil,CGRectGetMinX(bounds),CGRectGetMinY

    (bounds));

    CGPathAddArcToPoint(pathRef,nil,CGRectGetMinX(bounds),CGRectGetMaxY(bounds),CGRectGetMidX(bounds),CGRectGetMaxY

    (bounds), cornerRadius);

    CGPathAddArcToPoint(pathRef,nil,CGRectGetMaxX(bounds),CGRectGetMaxY(bounds),CGRectGetMaxX(bounds),CGRectGetMidY

    (bounds), cornerRadius);

    CGPathAddLineToPoint(pathRef,nil,CGRectGetMaxX(bounds),CGRectGetMinY

    (bounds));

    }

    else

    {

    CGPathAddRect(pathRef,nil

    , bounds);

    addLine =

    YES

    ;

    }

    layer.

    path

    = pathRef;

    CFRelease

    (pathRef);

    layer.

    fillColor= [UIColorcolorWithWhite:1.falpha:0.8f].CGColor

    ;

    if(addLine ==YES

    ) {

    CALayer*lineLayer = [[CALayeralloc]init

    ];

    CGFloatlineHeight = (1.f/ [UIScreenmainScreen].scale

    );

    lineLayer.

    frame=CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10

    , lineHeight);

    lineLayer.

    backgroundColor= tableView.separatorColor.CGColor

    ;

    [layer

    addSublayer

    :lineLayer];

    }

    UIView*testView = [[UIViewalloc]initWithFrame

    :bounds];

    [testView.

    layerinsertSublayer:layeratIndex:0

    ];

    testView.

    backgroundColor=UIColor.clearColor

    ;

    cell.backgroundView= testView;

    } } }

    相关文章

      网友评论

        本文标题:iOS如何设置tableView分组样式cell圆角

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