Skip to content

Commit 28be1e5

Browse files
committed
5.0.7
1 parent d7f932a commit 28be1e5

8 files changed

Lines changed: 111 additions & 82 deletions

Sources/UITableViewDynamicLayoutCacheHeight/UITableView+BMDynamicLayout.m

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,34 @@ - (UIView *)_cellViewWithCellClass:(Class)clas {
8888
dict = @{}.mutableCopy;
8989
objc_setAssociatedObject(self, _cmd, dict, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
9090
}
91-
9291
UIView *view = dict[NSStringFromClass(clas)];
93-
94-
if (!view) {
95-
NSBundle *bundle = [NSBundle bundleForClass:clas];
96-
NSString *path = [bundle pathForResource:NSStringFromClass(clas) ofType:@"nib"];
97-
UITableViewCell *cell = nil;
98-
if (path.length > 0) {
99-
NSArray <UITableViewCell *> *arr = [bundle loadNibNamed:NSStringFromClass(clas) owner:nil options:nil];
100-
for (UITableViewCell *obj in arr) {
101-
if ([obj isMemberOfClass:clas]) {
102-
cell = obj;
103-
// 清空 reuseIdentifier
104-
[cell setValue:nil forKey:@"reuseIdentifier"];
105-
break;
106-
}
92+
if (view) {
93+
// 直接返回
94+
return view;;
95+
}
96+
97+
NSBundle *bundle = [NSBundle bundleForClass:clas];
98+
NSString *path = [bundle pathForResource:NSStringFromClass(clas) ofType:@"nib"];
99+
UITableViewCell *cell = nil;
100+
if (path.length > 0) {
101+
NSArray <UITableViewCell *> *arr = [bundle loadNibNamed:NSStringFromClass(clas) owner:nil options:nil];
102+
for (UITableViewCell *obj in arr) {
103+
if ([obj isMemberOfClass:clas]) {
104+
cell = obj;
105+
// 清空 reuseIdentifier
106+
[cell setValue:nil forKey:@"reuseIdentifier"];
107+
break;
107108
}
108109
}
109-
if (!cell) {
110-
cell = [[clas alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
111-
}
112-
view = [UIView new];
113-
[view addSubview:cell];
114-
dict[NSStringFromClass(clas)] = view;
115110
}
111+
if (!cell) {
112+
// 这里使用默认的 UITableViewCellStyleDefault 类型。
113+
// 如果需要自定义高度,通常都是使用的此类型, 暂时不考虑其他。
114+
cell = [[clas alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
115+
}
116+
view = [UIView new];
117+
[view addSubview:cell];
118+
dict[NSStringFromClass(clas)] = view;
116119
return view;
117120
}
118121

@@ -163,39 +166,46 @@ - (CGFloat)_heightWithCellClass:(Class)clas
163166

164167
#pragma mark - private HeaderFooterView
165168

166-
- (CGFloat)_heightWithHeaderFooterViewClass:(Class)clas
167-
sel:(SEL)sel
168-
configuration:(BMConfigurationHeaderFooterViewBlock)configuration {
169+
- (UIView *)_headerFooterViewWithHeaderFooterViewClass:(Class)clas
170+
sel:(SEL)sel {
169171
NSMutableDictionary *dict = objc_getAssociatedObject(self, sel);
170172
if (!dict) {
171173
dict = @{}.mutableCopy;
172174
objc_setAssociatedObject(self, sel, dict, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
173175
}
174-
175176
UIView *view = dict[NSStringFromClass(clas)];
176-
if (!view) {
177-
NSBundle *bundle = [NSBundle bundleForClass:clas];
178-
NSString *path = [bundle pathForResource:NSStringFromClass(clas) ofType:@"nib"];
179-
UIView *headerView = nil;
180-
if (path.length > 0) {
181-
NSArray <UITableViewHeaderFooterView *> *arr = [bundle loadNibNamed:NSStringFromClass(clas) owner:nil options:nil];
182-
for (UITableViewHeaderFooterView *obj in arr) {
183-
if ([obj isMemberOfClass:clas]) {
184-
headerView = obj;
185-
// 清空 reuseIdentifier
186-
[headerView setValue:nil forKey:@"reuseIdentifier"];
187-
break;
188-
}
177+
if (view) {
178+
// 直接返回
179+
return view;
180+
}
181+
182+
NSBundle *bundle = [NSBundle bundleForClass:clas];
183+
NSString *path = [bundle pathForResource:NSStringFromClass(clas) ofType:@"nib"];
184+
UIView *headerView = nil;
185+
if (path.length > 0) {
186+
NSArray <UITableViewHeaderFooterView *> *arr = [bundle loadNibNamed:NSStringFromClass(clas) owner:nil options:nil];
187+
for (UITableViewHeaderFooterView *obj in arr) {
188+
if ([obj isMemberOfClass:clas]) {
189+
headerView = obj;
190+
// 清空 reuseIdentifier
191+
[headerView setValue:nil forKey:@"reuseIdentifier"];
192+
break;
189193
}
190194
}
191-
if (!headerView) {
192-
headerView = [[clas alloc] initWithReuseIdentifier:nil];
193-
}
194-
view = [UIView new];
195-
[view addSubview:headerView];
196-
dict[NSStringFromClass(clas)] = view;
197195
}
196+
if (!headerView) {
197+
headerView = [[clas alloc] initWithReuseIdentifier:nil];
198+
}
199+
view = [UIView new];
200+
[view addSubview:headerView];
201+
dict[NSStringFromClass(clas)] = view;
202+
return view;
203+
}
198204

205+
- (CGFloat)_heightWithHeaderFooterViewClass:(Class)clas
206+
sel:(SEL)sel
207+
configuration:(BMConfigurationHeaderFooterViewBlock)configuration {
208+
UIView *view = [self _headerFooterViewWithHeaderFooterViewClass:clas sel:sel];
199209
// 获取 TableView 宽度
200210
UIView *temp = self.superview ? self.superview : self;
201211
tableViewDynamicLayoutLayoutIfNeeded(temp);

Sources/UITableViewDynamicLayoutCacheHeight/UITableViewCell+BMDynamicLayout.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@
2828
/// 请把此值设置为 YES,可提升一定的性能。
2929
@property (nonatomic, assign) IBInspectable BOOL bm_maxYViewFixed;
3030

31-
/// 免注册直接创建 Cell,内部自动处理取缓存池操作
31+
/// 免注册 IB 创建 UITableViewCell,内部自动处理缓冲池。
3232
/// @param tableView tableView
33-
+ (instancetype)bm_tableViewCellWithTableView:(UITableView *)tableView;
33+
+ (instancetype)bm_tableViewCellFromNibWithTableView:(UITableView *)tableView;
3434

35-
/// 免注册直接创建 Cell,内部自动处理取缓存池操作
35+
/// 免注册 alloc 创建 UITableViewCell,内部自动处理缓冲池, 默认 UITableViewCellStyleDefault 类型
3636
/// @param tableView tableView
37-
/// @param style cell 类型
38-
+ (instancetype)bm_tableViewCellWithTableView:(UITableView *)tableView style:(UITableViewCellStyle)style;
37+
+ (instancetype)bm_tableViewCellFromAllocWithTableView:(UITableView *)tableView;
38+
39+
/// 免注册 alloc 创建 UITableViewCell,内部自动处理缓冲池。
40+
/// @param tableView tableView
41+
/// @param style cell style
42+
+ (instancetype)bm_tableViewCellFromAllocWithTableView:(UITableView *)tableView style:(UITableViewCellStyle)style;
3943

4044
@end

Sources/UITableViewDynamicLayoutCacheHeight/UITableViewCell+BMDynamicLayout.m

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,33 @@ - (void)setBm_maxYViewFixed:(BOOL)bm_maxYViewFixed {
3333
objc_setAssociatedObject(self, @selector(bm_maxYViewFixed), @(bm_maxYViewFixed), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
3434
}
3535

36-
+ (instancetype)bm_tableViewCellWithTableView:(UITableView *)tableView {
37-
return [self bm_tableViewCellWithTableView:tableView style:UITableViewCellStyleDefault];
38-
}
39-
40-
+ (instancetype)bm_tableViewCellWithTableView:(UITableView *)tableView style:(UITableViewCellStyle)style {
36+
+ (instancetype)bm_tableViewCellFromNibWithTableView:(UITableView *)tableView {
4137
NSString *selfClassName = NSStringFromClass(self.class);
4238
NSString *reuseIdentifier = [selfClassName stringByAppendingString:@"BMDynamicLayoutReuseIdentifier"];
4339
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
4440
if (cell) {
4541
return cell;
4642
}
4743
NSBundle *bundle = [NSBundle bundleForClass:self.class];
48-
NSString *path = [bundle pathForResource:selfClassName ofType:@"nib"];
49-
if (path.length) {
50-
NSArray <UITableViewCell *> *arr = [bundle loadNibNamed:selfClassName owner:nil options:nil];
51-
for (UITableViewCell *obj in arr) {
52-
if ([obj isMemberOfClass:self.class]) {
53-
cell = obj;
54-
[cell setValue:reuseIdentifier forKey:@"reuseIdentifier"];
55-
return cell;
56-
}
44+
NSArray <UITableViewCell *> *arr = [bundle loadNibNamed:selfClassName owner:nil options:nil];
45+
for (UITableViewCell *obj in arr) {
46+
if ([obj isMemberOfClass:self.class]) {
47+
cell = obj;
48+
[cell setValue:reuseIdentifier forKey:@"reuseIdentifier"];
49+
return cell;
5750
}
5851
}
59-
return [[self alloc] initWithStyle:style reuseIdentifier:selfClassName];
52+
NSAssert(NO, @"你的 Cell 不是 IB 创建的");
53+
return nil;
54+
}
55+
56+
+ (instancetype)bm_tableViewCellFromAllocWithTableView:(UITableView *)tableView {
57+
return [self bm_tableViewCellFromAllocWithTableView:tableView style:(UITableViewCellStyleDefault)];
58+
}
59+
60+
+ (instancetype)bm_tableViewCellFromAllocWithTableView:(UITableView *)tableView style:(UITableViewCellStyle)style {
61+
NSString *reuseIdentifier = [NSStringFromClass(self.class) stringByAppendingString:@"BMDynamicLayoutReuseIdentifier"];
62+
return [[self alloc] initWithStyle:style reuseIdentifier:reuseIdentifier];
6063
}
6164

6265
@end

Sources/UITableViewDynamicLayoutCacheHeight/UITableViewDynamicLayoutCacheHeight.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// SOFTWARE.
2222

2323
////////////////
24-
/// v5.0.6
24+
/// v5.0.7
2525
////////////////
2626

2727
#ifndef UITableViewDynamicLayoutCacheHeight_h

Sources/UITableViewDynamicLayoutCacheHeight/UITableViewHeaderFooterView+BMDynamicLayout.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
/// 请把此值设置为 YES,可提升一定的性能。
2929
@property (nonatomic, assign) IBInspectable BOOL bm_maxYViewFixed;
3030

31-
/// 免注册直接创建 UITableViewHeaderFooterView,内部自动处理取缓存池操作
31+
/// 免注册 IB 创建 UITableViewHeaderFooterView,内部自动处理缓冲池。
3232
/// @param tableView tableView
33-
+ (instancetype)bm_tableViewHeaderFooterViewWithTableView:(UITableView *)tableView;
33+
+ (instancetype)bm_tableViewHeaderFooterViewFromNibWithTableView:(UITableView *)tableView;
34+
35+
/// 免注册 alloc 创建 UITableViewHeaderFooterView,内部自动处理缓冲池。
36+
/// @param tableView tableView
37+
+ (instancetype)bm_tableViewHeaderFooterViewFromAllocWithTableView:(UITableView *)tableView;
3438

3539
@end

Sources/UITableViewDynamicLayoutCacheHeight/UITableViewHeaderFooterView+BMDynamicLayout.m

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,34 @@ - (void)setBm_maxYViewFixed:(BOOL)bm_maxYViewFixed {
3333
objc_setAssociatedObject(self, @selector(bm_maxYViewFixed), @(bm_maxYViewFixed), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
3434
}
3535

36-
+ (instancetype)bm_tableViewHeaderFooterViewWithTableView:(UITableView *)tableView {
36+
+ (instancetype)bm_tableViewHeaderFooterViewFromNibWithTableView:(UITableView *)tableView {
3737
NSString *selfClassName = NSStringFromClass(self.class);
3838
NSString *reuseIdentifier = [selfClassName stringByAppendingString:@"BMDynamicLayoutReuseIdentifier"];
3939
UITableViewHeaderFooterView *headerFooterView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuseIdentifier];
4040
if (headerFooterView) {
4141
return headerFooterView;
4242
}
4343
NSBundle *bundle = [NSBundle bundleForClass:self.class];
44-
NSString *path = [bundle pathForResource:selfClassName ofType:@"nib"];
45-
if (path.length > 0) {
46-
NSArray <UITableViewHeaderFooterView *> *arr = [bundle loadNibNamed:selfClassName owner:nil options:nil];
47-
for (UITableViewHeaderFooterView *obj in arr) {
48-
if ([obj isMemberOfClass:self.class]) {
49-
headerFooterView = obj;
50-
[headerFooterView setValue:reuseIdentifier forKey:@"reuseIdentifier"];
51-
return headerFooterView;
52-
}
44+
NSArray <UITableViewHeaderFooterView *> *arr = [bundle loadNibNamed:selfClassName owner:nil options:nil];
45+
for (UITableViewHeaderFooterView *obj in arr) {
46+
if ([obj isMemberOfClass:self.class]) {
47+
headerFooterView = obj;
48+
[headerFooterView setValue:reuseIdentifier forKey:@"reuseIdentifier"];
49+
return headerFooterView;
5350
}
5451
}
55-
return [[self alloc] initWithReuseIdentifier:selfClassName];
52+
NSAssert(NO, @"你的 UITableViewHeaderFooterView 不是 IB 创建的");
53+
return nil;
54+
}
55+
56+
+ (instancetype)bm_tableViewHeaderFooterViewFromAllocWithTableView:(UITableView *)tableView {
57+
NSString *selfClassName = NSStringFromClass(self.class);
58+
NSString *reuseIdentifier = [selfClassName stringByAppendingString:@"BMDynamicLayoutReuseIdentifier"];
59+
UITableViewHeaderFooterView *headerFooterView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuseIdentifier];
60+
if (headerFooterView) {
61+
return headerFooterView;
62+
}
63+
return [[self alloc] initWithReuseIdentifier:reuseIdentifier];
5664
}
5765

5866
@end

UITableViewDynamicLayoutCacheHeight.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'UITableViewDynamicLayoutCacheHeight'
3-
s.version = '5.0.6'
3+
s.version = '5.0.7'
44
s.summary = '🖖 Template auto layout cell for automatically UITableViewCell UITableViewHeaderFooterView calculating and cache height framework.'
55
s.homepage = 'https://github.com/liangdahong/UITableViewDynamicLayoutCacheHeight'
66
s.license = 'MIT'

UITableViewDynamicLayoutCacheHeight/Classes/Controller/BMHomeVC.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
5656
}
5757

5858
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
59-
BMCell *cell = [BMCell bm_tableViewCellWithTableView:tableView];
59+
BMCell *cell = [BMCell bm_tableViewCellFromNibWithTableView:tableView];
6060
cell.model = self.dataArray[indexPath.section].modelArray[indexPath.row];
6161
return cell;
6262
}
@@ -76,7 +76,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
7676
#pragma mark - UITableViewHeaderFooterView
7777

7878
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
79-
BMHeaderView *view = [BMHeaderView bm_tableViewHeaderFooterViewWithTableView:tableView];
79+
BMHeaderView *view = [BMHeaderView bm_tableViewHeaderFooterViewFromNibWithTableView:tableView];
8080
view.titleLabel.text = self.dataArray[section].headerTitle;
8181
return view;
8282
}
@@ -90,7 +90,7 @@ - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSIntege
9090
}
9191

9292
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
93-
UIFooterView *view = [UIFooterView bm_tableViewHeaderFooterViewWithTableView:tableView];
93+
UIFooterView *view = [UIFooterView bm_tableViewHeaderFooterViewFromNibWithTableView:tableView];
9494
view.titleLabel.text = self.dataArray[section].footerTitle;
9595
return view;
9696
}

0 commit comments

Comments
 (0)