tableview 分組加陰影 圓角

區(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];
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容