iOS 管理separator的UITableViewCell子类

1
2
3
4
5
6
@interface ZDBaseTableViewCell : UITableViewCell

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

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@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子类
http://example.com/2017/12/20/iOS-管理separator的UITableViewCell子类/
作者
guanzhendong
发布于
2017年12月20日
许可协议