Skip to content

Commit bc39b42

Browse files
author
appmj
committed
gif
1 parent 61f0c22 commit bc39b42

File tree

20 files changed

+418
-33
lines changed

20 files changed

+418
-33
lines changed

Resources/0.gif

-2.48 MB
Binary file not shown.

Sources/BMTemplateLayoutCell/UITableView+BMTemplateLayoutCell.h

100644100755
Lines changed: 107 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,120 @@
1+
// MIT License
2+
//
13
// Copyright (c) ( https://github.com/liangdahong )
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
222

323
#import <UIKit/UIKit.h>
424

525
NS_ASSUME_NONNULL_BEGIN
626

27+
/**
28+
BMLayoutCellConfigurationBlock
29+
30+
@param cell 需要布局的Cell
31+
*/
732
typedef void (^BMLayoutCellConfigurationBlock)(__kindof UITableViewCell *cell);
833

34+
/**
35+
BMLayoutHeaderFooterViewConfigurationBlock
36+
37+
@param headerFooterView 需要布局的tableViewHeaderFooterView
38+
*/
939
typedef void (^BMLayoutHeaderFooterViewConfigurationBlock)(__kindof UITableViewHeaderFooterView *headerFooterView);
1040

1141
#pragma mark - BMTemplateLayoutCell
1242

43+
/**
44+
BMTemplateLayoutCell
45+
*/
1346
IB_DESIGNABLE
1447
@interface UITableView (BMTemplateLayoutCell)
1548

16-
@property (assign, nonatomic, getter=isScreenRotating) IBInspectable BOOL screenRotating;
17-
18-
- (CGFloat)bm_heightForCellWithCellClass:(Class)clas
19-
configuration:(BMLayoutCellConfigurationBlock)configuration;
20-
21-
- (CGFloat)bm_heightForCellWithCellClass:(Class)clas
22-
cacheByIndexPath:(NSIndexPath *)indexPath
23-
configuration:(BMLayoutCellConfigurationBlock)configuration;
24-
25-
- (CGFloat)bm_heightForCellWithCellClass:(Class)clas
26-
cacheByKey:(NSString *)key
27-
configuration:(BMLayoutCellConfigurationBlock)configuration;
49+
@property (assign, nonatomic, getter=isScreenRotating) IBInspectable BOOL screenRotating; ///< 是否支持屏幕旋转,默认NO
50+
51+
/**
52+
获取 Cell 的高度(内部不做任何缓存操作)
53+
54+
@param clas Cell Class
55+
@param configuration 布局block
56+
@return 应该的高度
57+
*/
58+
- (CGFloat)bm_heightForCellWithCellClass:(Class)clas configuration:(BMLayoutCellConfigurationBlock)configuration;
59+
60+
/**
61+
获取 Cell 的高度(内部自动处理缓存)
62+
63+
@param clas Cell Class
64+
@param indexPath 内部会使用 indexPath 做缓存
65+
@param configuration 布局block
66+
@return 应该的高度
67+
*/
68+
- (CGFloat)bm_heightForCellWithCellClass:(Class)clas cacheByIndexPath:(NSIndexPath *)indexPath configuration:(BMLayoutCellConfigurationBlock)configuration;
69+
70+
/**
71+
获取 Cell 的高度(内部自动处理缓存,推荐使用)
72+
73+
@param clas Cell Class
74+
@param key 唯一key,内部以此key来做缓存key
75+
@param configuration 布局block
76+
@return 应该的高度
77+
*/
78+
- (CGFloat)bm_heightForCellWithCellClass:(Class)clas cacheByKey:(NSString *)key configuration:(BMLayoutCellConfigurationBlock)configuration;
2879

2980
@end
3081

3182
#pragma mark - BMTemplateLayoutHeaderFooterView
3283

84+
/**
85+
BMTemplateLayoutHeaderFooterView
86+
*/
3387
@interface UITableView (BMTemplateLayoutHeaderFooterView)
3488

35-
- (CGFloat)bm_heightForHeaderFooterViewWithWithHeaderFooterViewClass:(Class)clas
36-
configuration:(BMLayoutHeaderFooterViewConfigurationBlock)configuration;
89+
/**
90+
获取 HeaderFooterView 的高度(内部不做任何缓存操作)
91+
92+
@param clas HeaderFooterView class
93+
@param configuration 布局block
94+
@return 应该的高度
95+
*/
96+
- (CGFloat)bm_heightForHeaderFooterViewWithWithHeaderFooterViewClass:(Class)clas configuration:(BMLayoutHeaderFooterViewConfigurationBlock)configuration;
97+
98+
/**
99+
获取 HeaderFooterView 的高度(内部自动处理缓存)
100+
101+
@param clas HeaderFooterView class
102+
@param isHeaderView 是否是表头(组的头部:YES 组的尾部:NO)
103+
@param section 内部会使用 section 做缓存
104+
@param configuration 布局block
105+
@return 应该的高度
106+
*/
107+
- (CGFloat)bm_heightForHeaderFooterViewWithWithHeaderFooterViewClass:(Class)clas isHeaderView:(BOOL)isHeaderView section:(NSInteger)section configuration:(BMLayoutHeaderFooterViewConfigurationBlock)configuration;
108+
109+
/**
110+
获取 HeaderFooterView 的高度(内部自动处理缓存)
111+
112+
@param clas HeaderFooterView class
113+
@param key 唯一key,内部会以次key来做缓存
114+
@param configuration 布局block
115+
@return 应该的高度
116+
*/
117+
- (CGFloat)bm_heightForHeaderFooterViewWithWithHeaderFooterViewClass:(Class)clas cacheByKey:(NSString *)key configuration:(BMLayoutHeaderFooterViewConfigurationBlock)configuration;
37118

38119
@end
39120

@@ -42,13 +123,25 @@ IB_DESIGNABLE
42123
IB_DESIGNABLE
43124
@interface UITableViewCell (BMTemplateLayoutCell)
44125

126+
/**
127+
最大Y的View是否为变化的,默认:NO(固定),如果Cell的最大Y的View是变化的请设置为YES,通常是在设置了2个最大View时使用。
128+
*/
45129
@property (assign, nonatomic, getter=isDynamicCellBottomView) IBInspectable BOOL dynamicCellBottomView;
130+
46131
+ (instancetype)bm_tableViewCellWithTableView:(UITableView *)tableView;
47132

48133
@end
49134

135+
#pragma mark - UITableViewHeaderFooterView BMTemplateLayoutCell
136+
137+
IB_DESIGNABLE
50138
@interface UITableViewHeaderFooterView (BMTemplateLayoutCell)
51139

140+
/**
141+
最大Y的View是否为变化的,默认:NO(固定),如果UITableViewHeaderFooterView的最大Y的View是变化的请设置为YES,通常是在设置了2个(更多)最大View时使用。
142+
*/
143+
@property (assign, nonatomic, getter=isDynamicHeaderFooterBottomView) IBInspectable BOOL dynamicHeaderFooterBottomView;
144+
52145
+ (instancetype)bm_tableViewHeaderFooterViewWithTableView:(UITableView *)tableView;
53146

54147
@end

Sources/BMTemplateLayoutCell/UITableView+BMTemplateLayoutCell.m

100644100755
Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1+
// MIT License
2+
//
13
// Copyright (c) ( https://github.com/liangdahong )
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
222

323
#import "UITableView+BMTemplateLayoutCell.h"
424
#import <objc/runtime.h>
@@ -127,6 +147,11 @@ - (UIView *)bm_tempViewCellWithCellClass:(Class)clas {
127147
}
128148

129149
- (CGFloat)bm_layoutIfNeededCellWith:(UITableViewCell *)cell configuration:(BMLayoutCellConfigurationBlock)configuration {
150+
151+
if (self.superview) {
152+
[self.superview layoutIfNeeded];
153+
}
154+
130155
cell.superview.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, 0.0f);
131156
cell.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, 0.0f);
132157
configuration(cell);
@@ -433,15 +458,16 @@ - (UIView *)bm_tempViewHeaderFooterViewWithHeaderFooterViewClass:(Class)clas {
433458
}
434459
UIView *tempView = objc_getAssociatedObject(self, (__bridge const void *)(noReuseIdentifierChar));
435460
if (!tempView) {
436-
461+
// 没有绑定就创建
437462
NSString *path = [[NSBundle mainBundle] pathForResource:NSStringFromClass(clas.class) ofType:@"nib"];
438463
UITableViewHeaderFooterView *noCachetableViewHeaderFooterView = nil;
439464
if (path.length) {
440465
noCachetableViewHeaderFooterView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(clas) owner:nil options:nil][0];
441466
} else {
442467
noCachetableViewHeaderFooterView = [[clas alloc] initWithReuseIdentifier:noReuseIdentifier];
443468
}
444-
469+
470+
// 绑定起来
445471
tempView = UIView.new;
446472
[tempView addSubview:noCachetableViewHeaderFooterView];
447473
objc_setAssociatedObject(self, (__bridge const void *)(noReuseIdentifierChar), tempView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
@@ -451,19 +477,55 @@ - (UIView *)bm_tempViewHeaderFooterViewWithHeaderFooterViewClass:(Class)clas {
451477
}
452478

453479
- (CGFloat)bm_layoutIfNeededHeaderFooterViewWith:(UITableViewHeaderFooterView *)tableViewHeaderFooterView configuration:(BMLayoutHeaderFooterViewConfigurationBlock)configuration {
480+
481+
if (self.superview) {
482+
[self.superview layoutIfNeeded];
483+
}
484+
454485
tableViewHeaderFooterView.superview.frame = CGRectMake(0, 0, self.frame.size.width, 0);
455486
tableViewHeaderFooterView.frame = CGRectMake(0, 0, self.frame.size.width, 0);
456487
configuration(tableViewHeaderFooterView);
457488
[tableViewHeaderFooterView.superview layoutIfNeeded];
458-
CGFloat maxY = 0.0f;
459-
bm_templateLayout_get_view_subviews_MaxY(tableViewHeaderFooterView, &maxY, nil);
460-
return maxY;
489+
if (!tableViewHeaderFooterView.isDynamicHeaderFooterBottomView) {
490+
// cell 的最底部View是固定的
491+
if (tableViewHeaderFooterView.linView) {
492+
// 如果有关联就可直接取最底部View的MaxY为Cell的高度
493+
return CGRectGetMaxY(tableViewHeaderFooterView.linView.frame);
494+
} else {
495+
// 还没有关联就遍历获取 MaxY,同时关联
496+
CGFloat maxY = 0.0f;
497+
UIView *v = nil;
498+
bm_templateLayout_get_view_subviews_MaxY(tableViewHeaderFooterView, &maxY, &v);
499+
tableViewHeaderFooterView.linView = v;
500+
return maxY;
501+
}
502+
} else {
503+
CGFloat maxY = 0.0f;
504+
bm_templateLayout_get_view_subviews_MaxY(tableViewHeaderFooterView, &maxY, nil);
505+
return maxY;
506+
}
461507
}
462508

463509
@end
464510

465511
@implementation UITableViewCell (BMTemplateLayoutCell)
466512

513+
- (BOOL)isDynamicCellBottomView {
514+
return [objc_getAssociatedObject(self, _cmd) boolValue];
515+
}
516+
517+
- (void)setDynamicCellBottomView:(BOOL)dynamicCellBottomView {
518+
objc_setAssociatedObject(self, @selector(isDynamicCellBottomView), @(dynamicCellBottomView), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
519+
}
520+
521+
- (UIView *)linView {
522+
return objc_getAssociatedObject(self, _cmd);
523+
}
524+
525+
- (void)setLinView:(UIView *)linView {
526+
objc_setAssociatedObject(self, @selector(linView), linView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
527+
}
528+
467529
+ (instancetype)bm_tableViewCellWithTableView:(UITableView *)tableView {
468530
NSString *selfClassName = NSStringFromClass(self.class);
469531
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:selfClassName];
@@ -480,12 +542,18 @@ + (instancetype)bm_tableViewCellWithTableView:(UITableView *)tableView {
480542
return cell;
481543
}
482544

483-
- (BOOL)isDynamicCellBottomView {
545+
@end
546+
547+
#pragma mark - UITableViewHeaderFooterView BMTemplateLayoutCell
548+
549+
@implementation UITableViewHeaderFooterView (BMTemplateLayoutCell)
550+
551+
- (BOOL)isDynamicHeaderFooterBottomView {
484552
return [objc_getAssociatedObject(self, _cmd) boolValue];
485553
}
486554

487-
- (void)setDynamicCellBottomView:(BOOL)dynamicCellBottomView {
488-
objc_setAssociatedObject(self, @selector(isDynamicCellBottomView), @(dynamicCellBottomView), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
555+
- (void)setDynamicHeaderFooterBottomView:(BOOL)dynamicHeaderFooterBottomView {
556+
objc_setAssociatedObject(self, @selector(isDynamicHeaderFooterBottomView), @(dynamicHeaderFooterBottomView), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
489557
}
490558

491559
- (UIView *)linView {
@@ -496,10 +564,6 @@ - (void)setLinView:(UIView *)linView {
496564
objc_setAssociatedObject(self, @selector(linView), linView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
497565
}
498566

499-
@end
500-
501-
@implementation UITableViewHeaderFooterView (BMTemplateLayoutCell)
502-
503567
+ (instancetype)bm_tableViewHeaderFooterViewWithTableView:(UITableView *)tableView {
504568
NSString *selfClassName = NSStringFromClass(self.class);
505569
UITableViewHeaderFooterView *headerFooterView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:selfClassName];
@@ -517,4 +581,3 @@ + (instancetype)bm_tableViewHeaderFooterViewWithTableView:(UITableView *)tableVi
517581
}
518582

519583
@end
520-

0 commit comments

Comments
 (0)