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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// GMBaseTableViewCell.m
// Gengmei
//
// Created by 翟国钧 on 15/1/14.
// Copyright (c) 2015年 Wanmeichuangyi. All rights reserved.
//
#import "GMTableViewCell.h"
#import "UIView+LineWithAutolayout.h"
#import <Masonry/Masonry.h>
#import <Constant.h>
#import <UIColor+GMTheme.h>
@implementation GMTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self lifeCycle];
}
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
[self lifeCycle];
}
- (void)lifeCycle{
[self setup];
// 保证在 cell layout 前有“全约束”
[self cellConstraints];
[self updateCellConstraints];
}
- (void)setup{
// 初始一个较大的 bounds,就不会出现在刚刚约束时就出现的 subView 与 contentView 的约束冲突
self.contentView.bounds = [UIScreen mainScreen].bounds;
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.accessoryType = UITableViewCellAccessoryNone;
_bottomLine = [self.contentView addBottomLine];
_detailDisclosure = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow"]];
_detailDisclosure.hidden = YES;
[self.contentView addSubview:_detailDisclosure];
[_detailDisclosure mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-15);
make.centerY.mas_equalTo(0);
make.size.sizeOffset(CGSizeMake(7, 11));
}];
_separatedView = [[UIView alloc] init];
_separatedView.hidden = YES;
_separatedView.backgroundColor = UIColor.background;
[self.contentView addSubview:_separatedView];
[_separatedView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.mas_equalTo(0);
make.height.mas_equalTo(10);
}];
}
- (void)prepareForReuse{
[super prepareForReuse];
// 在 cell 被复用时,也先设置一个较大的 bounds。因为如果即将 layout 的 subview 超出 contentView时,会发出的约束警告,有了这个 bounds,警告就不会出现了
self.contentView.bounds = [UIScreen mainScreen].bounds;
}
// 的iOS8下发现不重写这个 seperator 不生效
- (UIEdgeInsets)layoutMargins
{
return UIEdgeInsetsZero;
}
- (void)setShowArrow:(BOOL)showArrow{
_showArrow = showArrow;
_detailDisclosure.hidden = !_showArrow;
}
- (void)cellConstraints{
}
- (void)updateCellConstraints{
}
- (void)updateBottomLineConstraintsWithLeft:(CGFloat)left right:(CGFloat)right {
[_bottomLine mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(left);
make.right.mas_equalTo(right);
make.bottom.mas_equalTo(0);
make.height.mas_equalTo(ONE_PIXEL);
}];
}
@end
@implementation UITableViewCell (Reusable)
+ (NSString *)defaultReuseIdentifier {
return NSStringFromClass([self class]);
}
@end