區(qū)加陰影
_tableV.clipsToBounds = NO;
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//圓率
CGFloat cornerRadius = 5.0;
//大小
CGRect bounds = cell.bounds;
//行數(shù)
NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
CGRect shadowRect = CGRectZero;
if (indexPath.row == 0 && numberOfRows == 1) {
shadowRect = bounds;
} else if (indexPath.row == 0) {
shadowRect = CGRectMake(bounds.origin.x, bounds.origin.y - 2.5, bounds.size.width, 5);
cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
} else if (indexPath.row == numberOfRows - 1) {
shadowRect = CGRectMake(bounds.origin.x, bounds.size.height - 2.5, bounds.size.width, 5);
} else {
shadowRect = CGRectZero;
}
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:shadowRect];
cell.layer.masksToBounds = NO;
cell.layer.shadowColor = RGB(237, 237, 237).CGColor;
cell.layer.shadowOpacity = 0.75;
cell.layer.shadowRadius = cornerRadius;
cell.layer.shadowOffset = CGSizeZero;
cell.layer.shadowPath = bezierPath.CGPath;
}
區(qū)圓角
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
//圓率
CGFloat cornerRadius = 30.0;
//大小
CGRect bounds = cell.bounds;
//行數(shù)
NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
//繪制曲線
UIBezierPath *bezierPath = nil;
if (indexPath.row == 0 && numberOfRows == 1) {
//一個(gè)為一組時(shí),四個(gè)角都為圓角
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
} else if (indexPath.row == 0) {
//為組的第一行時(shí),左上、右上角為圓角
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
} else if (indexPath.row == numberOfRows - 1) {
//為組的最后一行,左下、右下角為圓角
bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
} else {
//中間的都為矩形
bezierPath = [UIBezierPath bezierPathWithRect:bounds];
}
//cell的背景色透明
cell.backgroundColor = [UIColor clearColor];
//新建一個(gè)圖層
CAShapeLayer *layer = [CAShapeLayer layer];
//圖層邊框路徑
layer.path = bezierPath.CGPath;
//圖層填充色,也就是cell的底色
layer.fillColor = [UIColor whiteColor].CGColor;
//圖層邊框線條顏色
/*
如果self.tableView.style = UITableViewStyleGrouped時(shí),每一組的首尾都會(huì)有一根分割線,目前我還沒找到去掉每組首尾分割線,保留cell分割線的辦法。
所以這里取巧,用帶顏色的圖層邊框替代分割線。
這里為了美觀,最好設(shè)為和tableView的底色一致。
設(shè)為透明,好像不起作用。
*/
layer.strokeColor = [UIColor whiteColor].CGColor;
//將圖層添加到cell的圖層中,并插到最底層
[cell.layer insertSublayer:layer atIndex:0];
}
}