88
99#import " ZHTableViewDataSource.h"
1010#import " ZHAutoConfigurationTableViewDelegate.h"
11- #import < objc/runtime.h>
1211
1312@interface ZHTableViewDataSource ()
1413
@@ -37,11 +36,19 @@ - (void)addGroupWithCompletionHandle:(ZHTableViewDataSourceAddGroupCompletionHan
3736}
3837
3938- (void )reloadTableViewData {
40- if (self.isAutoConfigurationTableViewDelegate ) {
41- _tableView.dataSource = self.autoConfiguration ;
42- _tableView.delegate = self.autoConfiguration ;
39+ if (!self.tableView .dataSource ) {
40+ if (self.isAutoConfigurationTableViewDelegate ) {
41+ self.tableView .dataSource = self.autoConfiguration ;
42+ } else {
43+ NSAssert (NO , @" 必须给 UITableView 设置 DataSource 代理" );
44+ }
45+ }
46+ if (!self.tableView .delegate ) {
47+ if (self.isAutoConfigurationTableViewDelegate ) {
48+ self.tableView .delegate = self.autoConfiguration ;
49+ }
4350 }
44- [self registerClasss ];
51+ [self registerClass ];
4552 [self .tableView reloadData ];
4653}
4754
@@ -85,13 +92,26 @@ + (CGFloat)heightForRowAtDataSource:(ZHTableViewDataSource *)dataSource
8592 if (!cell) {
8693 return 0 ;
8794 }
88- UITableViewCell *automaticHeightCell = [self cellForRowAtWithDataSource: dataSource indexPath: indexPath];
89- CGFloat automaticHeight = [dataSource automaticHeightWithView: automaticHeightCell
90- memberClass: [UITableViewCell class ]];
95+
96+ CGFloat automaticHeight = ({
97+ automaticHeight = CGFLOAT_MAX;
98+ if (cell.height == NSNotFound ) {
99+ UITableViewCell *automaticHeightCell = [self cellForRowAtWithDataSource: dataSource indexPath: indexPath];
100+ NSIndexPath *realyIndexPath = [self indexPathWithDataSource: dataSource
101+ indexPath: indexPath];
102+ [cell configCellWithCell: automaticHeightCell
103+ indexPath: realyIndexPath];
104+ automaticHeight = [automaticHeightCell sizeThatFits: CGSizeMake ([UIScreen mainScreen ].bounds.size.width, CGFLOAT_MAX)].height ;
105+ }
106+ automaticHeight;
107+ });
108+ CGFloat height = cell.height ;
91109 if (cell.height == NSNotFound && automaticHeight != CGFLOAT_MAX) {
92- cell. height = automaticHeight;
110+ height = automaticHeight;
93111 }
94- return [self heightWithCustomHandle: cell.height customCompletionHandle: customHeightCompletionHandle baseModel: cell];
112+ return [self heightWithCustomHandle: height
113+ customCompletionHandle: customHeightCompletionHandle
114+ baseModel: cell];
95115}
96116
97117+ (void )didSelectRowAtWithDataSource : (ZHTableViewDataSource *)dataSource
@@ -162,8 +182,7 @@ + (CGFloat)heightForHeaderFooterInSectionWithDataSource:(ZHTableViewDataSource *
162182 NSInteger height = 0 ;
163183 ZHTableViewBaseModel *baseModel;
164184 UITableViewHeaderFooterView *headFooter = [self viewHeaderFooterInSectionWithDtaSource: dataSource section: section style: style];
165- CGFloat automaticHeight = [dataSource automaticHeightWithView: headFooter
166- memberClass: [UITableViewHeaderFooterView class ]];
185+ CGFloat automaticHeight = [headFooter sizeThatFits: CGSizeMake ([UIScreen mainScreen ].bounds.size.width, CGFLOAT_MAX)].height ;
167186 switch (style) {
168187 case ZHTableViewHeaderFooterStyleHeader: {
169188 height = group.header .height ;
@@ -183,28 +202,38 @@ + (CGFloat)heightForHeaderFooterInSectionWithDataSource:(ZHTableViewDataSource *
183202 return [self heightWithCustomHandle: height customCompletionHandle: customHeightCompletionHandle baseModel: baseModel];
184203}
185204
186- - (CGFloat)automaticHeightWithView : (UIView *)view
187- memberClass : (Class )memberClass {
188- IMP imp1 = class_getMethodImplementation ([view class ], @selector (sizeThatFits: ));
189- IMP imp2 = class_getMethodImplementation (memberClass, @selector (sizeThatFits: ));
190- if (![view isMemberOfClass: memberClass] && imp1 != imp2) {
191- return [view sizeThatFits: CGSizeMake (CGRectGetWidth (self .tableView.frame), CGFLOAT_MAX)].height ;
192- }
193- return CGFLOAT_MAX;
194- }
195-
196205+ (CGFloat)heightWithCustomHandle : (CGFloat)height
197206 customCompletionHandle : (ZHTableViewDataSourceCustomHeightCompletionHandle)customCompletionHandle
198207 baseModel : (ZHTableViewBaseModel *)baseModel {
199- if (height != 0 ) {
200- return height;
201- }
202- if (customCompletionHandle) {
203- return customCompletionHandle (baseModel);
204- }
205- return 44 ;
208+ return [self lookBestHeightWithBlock: ^CGFloat (NSUInteger index, BOOL *stop) {
209+ if (index == 0 ) {
210+ return height;
211+ } else if (index == 1 && customCompletionHandle) {
212+ return customCompletionHandle (baseModel);
213+ } else {
214+ *stop = YES ;
215+ return 0 ;
216+ }
217+ }];
218+ }
219+
220+ + (BOOL )isVirifyHeight : (CGFloat)height {
221+ return height != NSNotFound && height != CGFLOAT_MAX;
206222}
207223
224+ + (CGFloat)lookBestHeightWithBlock : (CGFloat(^)(NSUInteger index, BOOL *stop))block {
225+ CGFloat height = 0 ;
226+ BOOL stop = NO ;
227+ NSUInteger index = 0 ;
228+ while (!stop) {
229+ height = block (index, &stop);
230+ if ([self isVirifyHeight: height]) {
231+ return height;
232+ }
233+ index ++;
234+ }
235+ return height;
236+ }
208237
209238+ (ZHTableViewGroup *)groupForSectionWithDataSource : (ZHTableViewDataSource *)dataSource
210239 section : (NSInteger )section {
@@ -247,7 +276,7 @@ + (void)dataSource:(ZHTableViewDataSource *)dataSource
247276 atIndexPath: indexPath];
248277}
249278
250- - (void )registerClasss {
279+ - (void )registerClass {
251280 for (ZHTableViewGroup *group in self.groups ) {
252281 [group registerHeaderFooterCellWithTableView: self .tableView];
253282 }
0 commit comments