【iOS開發】tableView-section圓角邊框解決方案
tableView圓角邊框解決方案
-
iOS 7之前,圖下圓角邊框很容易設置
-
iOS 7之后,tableviewcell的風格不再是圓角了
設置tableView中section圓角邊框,需求如下:
找了很多辦法都沒辦法解決。
-
設置過tableView的邊框,但是發現,滑動tableView的時候,其實是里面的content在移動,也就是,向上向下滑,邊框並不會動=。=。
-
可以試着打印tableView的frame,發現frame的x.y並不會移動。
最后解決了,找了兩種方案
【方案一】- 自定義cell
圓角邊框為UIImage,充當自定義cell的背景圖.
-
1.方法比較簡單,就不粘代碼了,弄上邊圓角,下邊圓角,沒有圓角,上下邊都有圓角四張圖片。
-
2.判斷加載的是section中的indexpath.row是幾,對應加載哪種圓角圖片,就可以了。
【方法二】- cell重繪
給section,繪制邊框。
-
1.在UITableViewDelegate有個展示cell的代理方法,在展示cell的時候,然后為section中的cell繪制邊框
-
2.index path.row0和index path.row最后一個 的時候,加圓角。
-
3.此方法,可以直接復制粘貼使用,另外,如果此需求用到的地方比較多,可以構建基類或者寫在category里面,方便使用代碼如下:
#pragma mark - Table view data source
// 重新繪制cell邊框
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(tintColor)]) {
// if (tableView == self.tableView) {
CGFloat cornerRadius = 10.f;
cell.backgroundColor = UIColor.clearColor;
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 10, 0);
BOOL addLine = NO;
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
} else if (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;
} else if (indexPath.row == [tableView numberOfRowsInSection: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 = [UIColor colorWithWhite:1.f alpha:0.5f].CGColor;
layer.strokeColor=[UIColor whiteColor].CGColor;
if (addLine == YES) {
CALayer *lineLayer = [[CALayer alloc] init];
CGFloat lineHeight = (1.f / [UIScreen mainScreen].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 = [[UIView alloc] initWithFrame:bounds];
[testView.layer insertSublayer:layer atIndex:0];
testView.backgroundColor = UIColor.clearColor;
cell.backgroundView = testView;
}
// }
}
來源http://www.jianshu.com/p/da1adec5a601