iOS 一行代码为TableView的Section设置圆角

效果图:

iOS 一行代码为TableView的Section设置圆角_第1张图片
Simulator Screen Shot 2017年8月2日 下午3.05.05.png

实现方法:
在cell的ContentView上添加一个View,设置第一个的上左,上右圆角。设置最后一个下左,下右圆角。其他不设置圆角。优点:可复用、简单
使用方法:
导入类别文件:UITableViewCell+SectionCorner
代理中添加方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestTableViewCell"];
    [cell addSectionCornerWithTableView:tableView indexPath:indexPath cornerViewframe:CGRectMake(20, 0, self.view.frame.size.width - 40, 44) cornerRadius:5];
    return cell;
}

源码:

#import 

@interface UITableViewCell (SectionCorner)

-(void)addSectionCornerWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath cornerViewframe:(CGRect)frame cornerRadius:(CGFloat)cornerRadius;

@property(nonatomic,strong) UIView *cornerV;
@property(nonatomic,strong) CAShapeLayer *topLay;
@property(nonatomic,strong) CAShapeLayer *bottomLay;
@end

#import "UITableViewCell+SectionCorner.h"

#import 

@implementation UITableViewCell (SectionCorner)
-(void)addSectionCornerWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath cornerViewframe:(CGRect)frame cornerRadius:(CGFloat)cornerRadius{
    
    if (self.cornerV == nil) {
        UIView *backV = [[UIView alloc] initWithFrame:frame];
        backV.backgroundColor = [UIColor redColor];
        backV.tag = 20170802;
        [self.contentView insertSubview:backV atIndex:0];
        self.cornerV = backV;
        self.backgroundColor = [UIColor clearColor];
    }
    if (indexPath.row == 0) {
        if (self.topLay == nil) {
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cornerV.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight  cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = self.cornerV.bounds;
            maskLayer.path = maskPath.CGPath;
            self.topLay = maskLayer;
        }
        self.cornerV.layer.mask = self.topLay;
    }else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1){
        if (self.bottomLay == nil) {
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cornerV.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight  cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = self.cornerV.bounds;
            maskLayer.path = maskPath.CGPath;
            self.bottomLay = maskLayer;
        }
       self.cornerV.layer.mask = self.bottomLay;
    }else{
        self.cornerV.layer.mask = nil;
    }
}

-(void)setCornerV:(UIView *)cornerV{
    objc_setAssociatedObject(self, @selector(cornerV), cornerV, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIView *)cornerV{
    return objc_getAssociatedObject(self, _cmd);
}
-(void)setTopLay:(CAShapeLayer *)topLay{
    objc_setAssociatedObject(self, @selector(topLay), topLay, OBJC_ASSOCIATION_RETAIN);
}
-(CAShapeLayer *)topLay{
    return objc_getAssociatedObject(self, _cmd);
}

-(void)setBottomLay:(CAShapeLayer *)bottomLay{
    objc_setAssociatedObject(self, @selector(bottomLay), bottomLay, OBJC_ASSOCIATION_RETAIN);
}
-(CAShapeLayer *)bottomLay{
    return objc_getAssociatedObject(self, _cmd);
}

@end

git地址:https://github.com/GitHubXuLiying/cornerBac

你可能感兴趣的:(iOS 一行代码为TableView的Section设置圆角)