iOS 管理separator的UITableViewCell子

作者: CodeGeass | 来源:发表于2017-11-28 11:13 被阅读35次
@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子

    本文链接:https://www.haomeiwen.com/subject/mtusbxtx.html