tableview 分组加阴影 圆角

区加阴影

        _tableV.clipsToBounds = NO;

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

        //圆率
        CGFloat cornerRadius = 5.0;
        //大小
        CGRect bounds = cell.bounds;
        //行数
        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;

}

区圆角

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        //圆率
        CGFloat cornerRadius = 30.0;
        //大小
        CGRect bounds = cell.bounds;
        //行数
        NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
        
        //绘制曲线
        UIBezierPath *bezierPath = nil;
        
        if (indexPath.row == 0 && numberOfRows == 1) {
            //一个为一组时,四个角都为圆角
            bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        } else if (indexPath.row == 0) {
            //为组的第一行时,左上、右上角为圆角
            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];
        //新建一个图层
        CAShapeLayer *layer = [CAShapeLayer layer];
        //图层边框路径
        layer.path = bezierPath.CGPath;
        //图层填充色,也就是cell的底色
        layer.fillColor = [UIColor whiteColor].CGColor;
        //图层边框线条颜色
        /*
         如果self.tableView.style = UITableViewStyleGrouped时,每一组的首尾都会有一根分割线,目前我还没找到去掉每组首尾分割线,保留cell分割线的办法。
         所以这里取巧,用带颜色的图层边框替代分割线。
         这里为了美观,最好设为和tableView的底色一致。
         设为透明,好像不起作用。
         */
        layer.strokeColor = [UIColor whiteColor].CGColor;
        //将图层添加到cell的图层中,并插到最底层
        [cell.layer insertSublayer:layer atIndex:0];
    }
}

你可能感兴趣的:(tableview 分组加阴影 圆角)