ios小技巧,实现UITableViewCell圆角

有时候我们需要把TableViewCell做成圆角,并且cell之间有一定间距,网上很多都是在tableView的contentView添加自己的View来实现,其实都不是特别好。那么现在有个小技巧,就是重新布局tableView的contentView。其代码如下:

- (void)awakeFromNib {
    
    [self setuplayout];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setuplayout];
    }
    return self;
}

- (void)setuplayout {
    
    self.contentView.layer.borderColor = [[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1.0] CGColor];
    self.contentView.layer.borderWidth = 1.0f;
    self.contentView.layer.cornerRadius = 5.0f;
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor whiteColor];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.contentView.frame = CGRectInset(self.bounds, 5, 5);
    //由于改了contentView.frame。所以这句话要在这里执行
    self.contentView.clipsToBounds = YES;
}

 
原文地址:https://www.cnblogs.com/visonhome/p/4482754.html