【方案一】- 自定義cell
** 圓角邊框為UIImage,充當(dāng)自定義cell的背景圖.**
方法比較簡單,就不粘代碼了帘不,弄上邊圓角,下邊圓角杨箭,沒有圓角寞焙,上下邊都有圓角四張圖片。
判斷加載的是section中的indexpath.row是幾,對應(yīng)加載哪種圓角圖片捣郊,就可以了辽狈,這種方法簡單,但是利用效率不高呛牲,代碼復(fù)用性也不高刮萌。
【方案二】- 給cell的contentView的layer加自定義邊框
//給cell的layer加邊框,這個是tableView的協(xié)議方法
- (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地址 明天上傳