圆角的tableView

---恢复内容开始---

效果:

圆角的tableView

实现原理,先画一个矩形,把这个矩形当作tableView的mask;代码:

//画一个圆角的矩形
- (void)drawRect:(CGRect)rect {
    //获取当前上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat cornerRadius = 10.0;
    CGFloat minx = CGRectGetMinX(rect);
    CGFloat midx = CGRectGetMidX(rect);
    CGFloat maxx = CGRectGetMaxX(rect);
    CGFloat miny = CGRectGetMinY(rect);
    CGFloat midy = CGRectGetMidY(rect);
    CGFloat maxy = CGRectGetMaxY(rect);
    
    CGContextMoveToPoint(context, minx, midy);
    CGContextAddArcToPoint(context, minx, miny, midx, miny, cornerRadius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, cornerRadius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, cornerRadius);
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, cornerRadius);
    
    CGContextClosePath(context);
    CGContextFillPath(context);
}
- (void)setupView {
    mask = [[ConnerView alloc]initWithFrame:CGRectZero];
    self.layer.mask = mask.layer;
    self.layer.cornerRadius = 10.0;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setupView];
    }
    return self;
}

- (void)adjustMask {
    CGRect frame = mask.frame;
    if (self.contentSize.height > self.frame.size.height) {
        frame.size = self.contentSize;
    }else {
        frame.size = self.frame.size;
    }
    mask.frame = frame;
    [mask setNeedsLayout];
    [self setNeedsLayout];
}

- (void)reloadData {
    [super reloadData];
    [self adjustMask];
}
#pragma mark delegata
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}