iOS 管理separator的UITableViewCell子类

@interface ZDBaseTableViewCell : UITableViewCell

@property (nonatomic, assign) BOOL showsSeparator;// default is YES
@property (nonatomic, assign) CGFloat separatorLeading;// default is 15.f

@end
@interface ZDBaseTableViewCell ()

@property (nonatomic, strong) CALayer *separatorLayer;

@end

@implementation ZDBaseTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

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

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

- (void)setup {
    // 默认15
    _separatorLeading = 15.f;
    _showsSeparator = YES;
    
    // 设置选中颜色
//    self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
//    self.selectedBackgroundView.backgroundColor = [UIColor zd_separatorColor];
    
    // 设置分隔线
    _separatorLayer = [[CALayer alloc] init];
    _separatorLayer.backgroundColor = [UIColor zd_separatorColor].CGColor;
    [self.layer addSublayer:_separatorLayer];
}

- (void)setShowsSeparator:(BOOL)showsSeparator {
    _showsSeparator = showsSeparator;
    _separatorLayer.hidden = !showsSeparator;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (_showsSeparator) {
        [UIView setAnimationsEnabled:NO];
        self.separatorLayer.frame = CGRectMake(_separatorLeading, CGRectGetHeight(self.bounds) - SINGLE_LINE_ADJUST_OFFSET, CGRectGetWidth(self.bounds) - _separatorLeading, SINGLE_LINE_HEIGHT);
        [UIView setAnimationsEnabled:YES];
    }
}

@end

你可能感兴趣的:(iOS 管理separator的UITableViewCell子类)