Skip to content

Commit bf28aee

Browse files
author
张行
committed
❇️新增
- 新增 UITableView scrollViewDidScroll 和 scrollViewWillBeginDragging 支持 - 新增单独进行注册 Cell 方法 - 新增可以单独给 UITableView 设置 Delegate 源 🔴修复 - 修复关闭自动设置代理问题 - 修复UICollectionViewCell只有自定义高度不为0 不是 NSNotFound 不等于 CGFlOAT_MAX 则返回自定义高度 - 修复 UITableViewCell 支持 sizeToFit 方法获取高度
1 parent ccf6a82 commit bf28aee

6 files changed

Lines changed: 100 additions & 40 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

ZHTableViewGroup/Classes/ZHAutoConfigurationTableViewDelegate.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)ce
7979
[ZHTableViewDataSource dataSource:_dataSource willDisplayCell:cell forRowAtIndexPath:indexPath];
8080
}
8181

82+
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
83+
if (_dataSource.scrollViewDidScrollCompletionHandle) {
84+
_dataSource.scrollViewDidScrollCompletionHandle(scrollView);
85+
}
86+
}
87+
88+
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
89+
if (_dataSource.scrollViewWillBeginDraggingCompletionHandle) {
90+
_dataSource.scrollViewWillBeginDraggingCompletionHandle(scrollView);
91+
}
92+
}
93+
8294
@end

ZHTableViewGroup/Classes/ZHCollectionViewDataSource.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@ @implementation ZHCollectionViewDataSource {
2424
- (instancetype)initWithCollectionView:(UICollectionView *)CollectionView {
2525
if (self = [super init]) {
2626
_collectionView = CollectionView;
27-
_autoConfigurationCollectionViewDelegate = YES;
27+
self.autoConfigurationCollectionViewDelegate = YES;
2828
}
2929
return self;
3030
}
3131

32+
- (void)setAutoConfigurationCollectionViewDelegate:(BOOL)autoConfigurationCollectionViewDelegate {
33+
_autoConfigurationCollectionViewDelegate = autoConfigurationCollectionViewDelegate;
34+
if (autoConfigurationCollectionViewDelegate) {
35+
_collectionView.dataSource = self.autoConfiguration;
36+
_collectionView.delegate = self.autoConfiguration;
37+
}
38+
}
39+
3240
- (void)addGroupWithCompletionHandle:(ZHCollectionViewDataSourceAddGroupCompletionHandle)completionHandle {
3341
ZHCollectionViewGroup *group = [[ZHCollectionViewGroup alloc] init];
3442
if (completionHandle) {
@@ -38,12 +46,9 @@ - (void)addGroupWithCompletionHandle:(ZHCollectionViewDataSourceAddGroupCompleti
3846
}
3947

4048
- (void)reloadCollectionViewData {
41-
if (self.isAutoConfigurationCollectionViewDelegate) {
42-
_collectionView.dataSource = self.autoConfiguration;
43-
_collectionView.delegate = self.autoConfiguration;
44-
}
4549
[self registerClasss];
4650
[self.collectionView reloadData];
51+
4752
}
4853

4954
+ (NSInteger)numberOfRowsInSectionWithDataSource:(ZHCollectionViewDataSource *)dataSource
@@ -113,7 +118,7 @@ + (UICollectionReusableView *)viewHeaderFooterInSectionWithDtaSource:(ZHCollecti
113118
+ (CGFloat)heightWithCustomHandle:(CGFloat)height
114119
customCompletionHandle:(ZHCollectionViewDataSourceCustomHeightCompletionHandle)customCompletionHandle
115120
baseModel:(ZHCollectionViewBaseModel *)baseModel {
116-
if (height != 0) {
121+
if (height != 0 && height != NSNotFound && height != CGFLOAT_MAX) {
117122
return height;
118123
}
119124
if (customCompletionHandle) {

ZHTableViewGroup/Classes/ZHTableViewDataSource.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#import "ZHTableViewGroup.h"
1111
#import "ZHAutoConfigurationTableViewDelegate.h"
1212

13-
1413
/**
1514
添加分组的回调
1615
@@ -40,9 +39,6 @@ typedef CGFloat (^ZHTableViewDataSourceCustomHeightCompletionHandle)(ZHTableView
4039
*/
4140
@property (nonatomic, assign) BOOL isWillDisplayData;
4241

43-
/**
44-
* 数据组
45-
*/
4642
@property (nonatomic, strong, readonly) NSMutableArray<ZHTableViewGroup *> *groups;
4743

4844
/**
@@ -60,6 +56,8 @@ typedef CGFloat (^ZHTableViewDataSourceCustomHeightCompletionHandle)(ZHTableView
6056
*/
6157
- (void)addGroupWithCompletionHandle:(ZHTableViewDataSourceAddGroupCompletionHandle)completionHandle;
6258

59+
- (void)registerClass;
60+
6361
/**
6462
进行刷新 UITableView
6563
*/
@@ -155,4 +153,10 @@ typedef CGFloat (^ZHTableViewDataSourceCustomHeightCompletionHandle)(ZHTableView
155153

156154
+ (void)dataSource:(ZHTableViewDataSource *)dataSource willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
157155

156+
#pragma mark - Delegate Block
157+
/* UITableView 滑动, scrollViewDidScroll的代理 */
158+
@property (nonatomic, copy) void (^scrollViewDidScrollCompletionHandle)(UIScrollView *scrollView);
159+
/* UITableView 滑动,scrollViewWillBeginDragging的代理 */
160+
@property (nonatomic, copy) void (^scrollViewWillBeginDraggingCompletionHandle)(UIScrollView *scrollView);
161+
158162
@end

ZHTableViewGroup/Classes/ZHTableViewDataSource.m

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
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
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// ZHTableViewGroupObjc.h
3+
// ZHTableViewGroupObjc
4+
//
5+
// Created by 张行 on 2018/6/7.
6+
// Copyright © 2018年 张行. All rights reserved.
7+
//
8+
9+
#import "ZHTableViewDataSource.h"
10+
#import "ZHCollectionViewDataSource.h"

0 commit comments

Comments
 (0)