al
liurenjie
2021-06-26 afc4a909befeb08739f73c7ef59fd1bf1b870287
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
//  UITableView+SDAutoTableViewCellHeight.h
//  SDAutoLayout 测试 Demo
//
//  Created by aier on 15/11/1.
//  Copyright © 2015年 gsd. All rights reserved.
//
 
/*
 
 *********************************************************************************
 *                                                                                *
 * 在您使用此自动布局库的过程中如果出现bug请及时以以下任意一种方式联系我们,我们会及时修复bug并  *
 * 帮您解决问题。                                                                    *
 * QQ    : 2689718696(gsdios)                                                      *
 * Email : gsdios@126.com                                                          *
 * GitHub: https://github.com/gsdios                                               *
 * 新浪微博:GSD_iOS                                                                 *
 *                                                                                *
 *********************************************************************************
 
 */
 
 
 
 
/*
 PS:cell高度自适应前提>>应该调用cell的“- (void)setupAutoHeightWithBottomView:(UIView *)bottomView bottomMargin:(CGFloat)bottomMargin”方法进行cell的自动高度设置
 */
 
#import <UIKit/UIKit.h>
 
#import "UIView+SDAutoLayout.h"
 
@class SDCellAutoHeightManager;
 
typedef void (^AutoCellHeightDataSettingBlock)(UITableViewCell *cell);
 
#define kSDModelCellTag 199206
 
 
 
#pragma mark - UITableView 方法,返回自动计算出的cell高度
 
@interface UITableView (SDAutoTableViewCellHeight)
 
@property (nonatomic, strong) SDCellAutoHeightManager *cellAutoHeightManager;
 
 
/**
 * 返回计算出的cell高度(普通简化版方法,同样只需一步设置即可完成)(用法:单cell详见demo5,多cell详见demo7)
 * model              : cell的数据模型实例
 * keyPath            : cell的数据模型属性的属性名字符串(即kvc原理中的key)
 * cellClass          : 当前的indexPath对应的cell的class
 * contentViewWidth   : cell的contentView的宽度
 */
- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath model:(id)model keyPath:(NSString *)keyPath cellClass:(Class)cellClass contentViewWidth:(CGFloat)contentViewWidth;
 
/**
 * 返回计算出的cell高度(普通简化版方法,同样只需一步设置即可完成)(用法:见DemoVC14)
 * cellClass          : 当前的indexPath对应的cell的class
 * contentViewWidth   : cell的contentView的宽度
 * cellDataSetting    : 设置cell数据的block
 */
- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath cellClass:(Class)cellClass cellContentViewWidth:(CGFloat)width cellDataSetting:(AutoCellHeightDataSettingBlock)cellDataSetting;
 
/** 刷新tableView但不清空之前已经计算好的高度缓存,用于直接将新数据拼接在旧数据之后的tableView刷新 */
- (void)reloadDataWithExistedHeightCache;
 
/** 刷新tableView同时调整已经计算好的高度缓存,用于直接将新数据插在旧数据前面的tableView的刷新 */
- (void)reloadDataWithInsertingDataAtTheBeginingOfSection:(NSInteger)section newDataCount:(NSInteger)count;
 
/** 
 * 刷新tableView同时调整已经计算好的高度缓存,用于直接将新数据插在旧数据前面的tableView的刷新(用于刷新多个section)
 * sectionNumsArray : 要刷新的所有section序号组成的数组, 例@[@(0), @(1)]
 * dataCountsArray  : 每个section的数据条数组成的数组, 例@[@(20), @(10)]
 */
- (void)reloadDataWithInsertingDataAtTheBeginingOfSections:(NSArray *)sectionNumsArray newDataCounts:(NSArray *)dataCountsArray;
 
/** 返回所有cell的高度总和  */
- (CGFloat)cellsTotalHeight;
 
@property (nonatomic, copy) AutoCellHeightDataSettingBlock cellDataSetting;
 
@end
 
 
 
 
#pragma mark - UITableViewController 方法,返回自动计算出的cell高度
 
@interface UITableViewController (SDTableViewControllerAutoCellHeight)
 
/** (UITableViewController方法)升级版!一行代码(一步设置)搞定tableview的cell高度自适应,同时适用于单cell和多cell,性能比普通版稍微差一些,不建议在数据量大的tableview中使用  */
- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath cellContentViewWidth:(CGFloat)width;
 
@end
 
 
 
#pragma mark - NSObject 方法,返回自动计算出的cell高度
 
@interface NSObject (SDAnyObjectAutoCellHeight)
 
/** (NSObject方法)升级版!一行代码(一步设置)搞定tableview的cell高度自适应,同时适用于单cell和多cell,性能比普通版稍微差一些,不建议在数据量大的tableview中使用  */
- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath cellContentViewWidth:(CGFloat)width tableView:(UITableView *)tableView;
 
@end
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
// ------------------------------- 以下为库内部使用无须了解 --------------------
 
@interface SDCellAutoHeightManager : NSObject
 
@property (nonatomic, assign) BOOL shouldKeepHeightCacheWhenReloadingData;
 
@property (nonatomic, assign) CGFloat contentViewWidth;
 
@property (nonatomic, assign) Class cellClass;
 
@property (nonatomic, assign) CGFloat cellHeight;
 
@property (nonatomic, strong) UITableViewCell *modelCell;
 
@property (nonatomic, strong) NSMutableDictionary *subviewFrameCacheDict;
 
@property (nonatomic, strong, readonly) NSDictionary *heightCacheDict;
 
@property (nonatomic, copy) AutoCellHeightDataSettingBlock cellDataSetting;
 
- (void)clearHeightCache;
 
- (void)clearHeightCacheOfIndexPaths:(NSArray *)indexPaths;
 
- (void)deleteThenResetHeightCache:(NSIndexPath *)indexPathToDelete;
 
- (void)insertNewDataAtTheBeginingOfSection:(NSInteger)section newDataCount:(NSInteger)count;
 
- (void)insertNewDataAtIndexPaths:(NSArray *)indexPaths;
 
- (NSNumber *)heightCacheForIndexPath:(NSIndexPath *)indexPath;
 
- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath model:(id)model keyPath:(NSString *)keyPath;
 
- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath model:(id)model keyPath:(NSString *)keyPath cellClass:(Class)cellClass;
 
 
- (NSMutableArray *)subviewFrameCachesWithIndexPath:(NSIndexPath *)indexPath;;
- (void)setSubviewFrameCache:(CGRect)rect WithIndexPath:(NSIndexPath *)indexPath;
 
- (instancetype)initWithCellClass:(Class)cellClass tableView:(UITableView *)tableView;
+ (instancetype)managerWithCellClass:(Class)cellClass tableView:(UITableView *)tableView;
@end