iOS cell圆角边框解决方案

【方案一】- 自定义cell
** 圆角边框为UIImage,充当自定义cell的背景图.**
方法比较简单,就不粘代码了,弄上边圆角,下边圆角,没有圆角,上下边都有圆角四张图片。
判断加载的是section中的indexpath.row是几,对应加载哪种圆角图片,就可以了,这种方法简单,但是利用效率不高,代码复用性也不高。

【方案二】- 给cell的contentView的layer加自定义边框

//给cell的layer加边框,这个是tableView的协议方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    CGMutablePathRef pathRef = CGPathCreateMutable();

    CGRect bounds = cell.bounds;
    CGFloat cornerRadius = 10.f;

    if (indexPath.row == 0) {//第一个cell
        //上边线
        CGPathMoveToPoint(pathRef, nil, bounds.origin.x + 10, bounds.origin.y + 20);
        //画弧
        CGPathAddArcToPoint(pathRef, nil, bounds.origin.x + 10, bounds.origin.y, bounds.origin.x + 30, bounds.origin.y, cornerRadius);
        //下面三句可以省略掉,写了更容易理解
//        CGPathMoveToPoint(pathRef, nil, bounds.origin.x + 20, bounds.origin.y);
//        CGPathAddLineToPoint(pathRef, nil, bounds.origin.x + bounds.size.width - 30, bounds.origin.y);
//        CGPathMoveToPoint(pathRef, nil,  bounds.origin.x + bounds.size.width - 30, bounds.origin.y);
        CGPathAddArcToPoint(pathRef, nil, bounds.origin.x + bounds.size.width - 10, bounds.origin.y, bounds.origin.x + bounds.size.width - 10, bounds.origin.y + 20, cornerRadius);
        
        //左边线
        CGPathMoveToPoint(pathRef, nil, bounds.origin.x + 10, bounds.origin.y + 20);
        CGPathAddLineToPoint(pathRef, nil, bounds.origin.x + 10, bounds.size.height);
        //右边线
        CGPathMoveToPoint(pathRef, nil, bounds.origin.x + bounds.size.width - 10, bounds.origin.y + 10);
        CGPathAddLineToPoint(pathRef, nil, bounds.origin.x + bounds.size.width - 10, bounds.size.height);
           }
    if(indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1){//最后一个cell
        //下边线
        CGPathMoveToPoint(pathRef, nil, bounds.origin.x + 10, bounds.origin.y + bounds.size.height);
        CGPathAddLineToPoint(pathRef, nil, bounds.origin.x + bounds.size.width - 10, bounds.origin.y + bounds.size.height);
        
        //左边线
        CGPathMoveToPoint(pathRef, nil, bounds.origin.x + 10, bounds.origin.y);
        CGPathAddLineToPoint(pathRef, nil, bounds.origin.x + 10, bounds.size.height);
        //右边线
        CGPathMoveToPoint(pathRef, nil, bounds.origin.x + bounds.size.width - 10, bounds.origin.y);
        CGPathAddLineToPoint(pathRef, nil, bounds.origin.x + bounds.size.width - 10, bounds.size.height);
    }
    if (indexPath.row != 0 && indexPath.row != [tableView numberOfRowsInSection:indexPath.section] - 1) {//中间的cell
        //左边线
        CGPathMoveToPoint(pathRef, nil, bounds.origin.x + 10, bounds.origin.y);
        CGPathAddLineToPoint(pathRef, nil, bounds.origin.x + 10, bounds.size.height);
        //右边线
        CGPathMoveToPoint(pathRef, nil, bounds.origin.x + bounds.size.width - 10, bounds.origin.y);
        CGPathAddLineToPoint(pathRef, nil, bounds.origin.x + bounds.size.width - 10, bounds.size.height);
    }
    layer.path = pathRef;
    CFRelease(pathRef);
    //颜色修改
    layer.fillColor = [UIColor whiteColor].CGColor;//这个是填充颜色,一个没有封闭的线条,无法做到完全填充
    layer.strokeColor=[UIColor redColor].CGColor;
    layer.borderWidth = 0.5;
    [cell.contentView.layer insertSublayer:layer atIndex:0];
}

上面的代码写的很灵活,都是加线等,可以按照项目具体需求具体更改,更改也比较方便
画弧的CGPathAddArcToPoint这个方法请参考http://blog.csdn.net/u012160319/article/details/44835353

demo正在上传中,demo地址 明天上传

你可能感兴趣的:(iOS cell圆角边框解决方案)