Skip to content

Commit 69ed7ca

Browse files
author
张行
committed
💚[新增一些代理的回调]
1 parent bf28aee commit 69ed7ca

8 files changed

Lines changed: 138 additions & 70 deletions

ZHTableViewGroup/Classes/ZHAutoConfigurationCollectionViewDelegate.m

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@
99
#import "ZHAutoConfigurationCollectionViewDelegate.h"
1010
#import "ZHCollectionViewDataSource.h"
1111

12+
@interface ZHAutoConfigurationCollectionViewDelegate ()
13+
14+
@property (nonatomic, weak) ZHCollectionViewDataSource *dataSource;
15+
16+
@end
17+
18+
1219
@implementation ZHAutoConfigurationCollectionViewDelegate {
13-
ZHCollectionViewDataSource *_dataSource;
20+
1421
}
1522

1623
- (instancetype)initWithDataSource:(ZHCollectionViewDataSource *)dataSource {
@@ -57,5 +64,28 @@ - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollection
5764
return [ZHCollectionViewDataSource referenceSizeForHeaderFooterWithDataSource:_dataSource style:ZHCollectionViewHeaderFooterStyleFooter section:section];
5865
}
5966

67+
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
68+
ZHCollectionViewGroup *group = _dataSource.groups[section];
69+
return group.sectionEdgeInsets;
70+
}
71+
72+
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
73+
if (_dataSource.scrollViewDidEndDraggingWillDecelerate) {
74+
_dataSource.scrollViewDidEndDraggingWillDecelerate(scrollView, decelerate);
75+
}
76+
77+
}
78+
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
79+
if (_dataSource.scrollViewDidEndDecelerating) {
80+
_dataSource.scrollViewDidEndDecelerating(scrollView);
81+
}
82+
}
83+
84+
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
85+
if (_dataSource.scrollViewDidEndScrollingAnimation) {
86+
_dataSource.scrollViewDidEndScrollingAnimation(scrollView);
87+
}
88+
}
89+
6090
@end
6191

ZHTableViewGroup/Classes/ZHAutoConfigurationTableViewDelegate.m

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
#import "ZHAutoConfigurationTableViewDelegate.h"
1010
#import "ZHTableViewDataSource.h"
1111

12-
@implementation ZHAutoConfigurationTableViewDelegate {
13-
ZHTableViewDataSource *_dataSource;
14-
}
12+
@interface ZHAutoConfigurationTableViewDelegate ()
13+
14+
@property (nonatomic, weak) ZHTableViewDataSource *dataSource;
15+
16+
@end
17+
18+
@implementation ZHAutoConfigurationTableViewDelegate
1519

1620
- (instancetype)initWithDataSource:(ZHTableViewDataSource *)dataSource {
1721
if (self = [super init]) {
@@ -36,15 +40,33 @@ - (CGFloat)tableView:(UITableView *)tableView
3640
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
3741
ZHTableViewDataSourceCustomHeightCompletionHandle handle = [self completionHandleWithTableView:tableView
3842
heightAtIndexPath:indexPath];
39-
return [ZHTableViewDataSource heightForRowAtDataSource:_dataSource
40-
indexPath:indexPath
41-
customHeightCompletionHandle:handle];
43+
CGFloat height = [ZHTableViewDataSource heightForRowAtDataSource:_dataSource
44+
indexPath:indexPath
45+
customHeightCompletionHandle:handle];
46+
if (_dataSource.heightForRowAtIndexPath) {
47+
_dataSource.heightForRowAtIndexPath(tableView, indexPath, height);
48+
}
49+
return height;
4250
}
43-
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
44-
return [ZHTableViewDataSource heightForHeaderInSectionWithDataSource:_dataSource section:section customHeightCompletionHandle:[self completionHandleWithTableView:tableView heightAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]]];
51+
- (CGFloat)tableView:(UITableView *)tableView
52+
heightForHeaderInSection:(NSInteger)section {
53+
CGFloat height = [ZHTableViewDataSource heightForHeaderInSectionWithDataSource:_dataSource
54+
section:section
55+
customHeightCompletionHandle:[self completionHandleWithTableView:tableView
56+
heightAtIndexPath:[NSIndexPath indexPathForRow:0
57+
inSection:section]]];
58+
if (_dataSource.heightForHeaderInSection) {
59+
_dataSource.heightForHeaderInSection(tableView, section, height);
60+
}
61+
return height;
4562
}
46-
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
47-
return [ZHTableViewDataSource heightForFooterInSectionWithDataSource:_dataSource section:section customHeightCompletionHandle:[self completionHandleWithTableView:tableView heightAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]]];
63+
- (CGFloat)tableView:(UITableView *)tableView
64+
heightForFooterInSection:(NSInteger)section {
65+
CGFloat height = [ZHTableViewDataSource heightForFooterInSectionWithDataSource:_dataSource section:section customHeightCompletionHandle:[self completionHandleWithTableView:tableView heightAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]]];
66+
if (_dataSource.heightForFooterInSection) {
67+
_dataSource.heightForFooterInSection(tableView, section, height);
68+
}
69+
return height;
4870
}
4971
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
5072
return [ZHTableViewDataSource viewForHeaderInSectionWithDataSource:_dataSource section:section];

ZHTableViewGroup/Classes/ZHCollectionViewDataSource.h

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,6 @@ typedef CGFloat (^ZHCollectionViewDataSourceCustomHeightCompletionHandle)(ZHColl
8282
*/
8383
+ (NSInteger)numberOfSectionsWithDataSource:(ZHCollectionViewDataSource *)dataSource;
8484

85-
/**
86-
获取Cell 所在索引的高度 如果autoConfigurationCollectionViewDelegate = YES 此方法不用调用
87-
88-
@param dataSource ZHCollectionViewDataSource数据源
89-
@param indexPath 索引
90-
@param customHeightCompletionHandle 自定义高度回调 实现 ZHCollectionViewCell 属性 cellHeight 无效
91-
@return Cell 的高度
92-
*/
93-
+ (CGFloat)heightForRowAtDataSource:(ZHCollectionViewDataSource *)dataSource indexPath:(NSIndexPath *)indexPath customHeightCompletionHandle:(ZHCollectionViewDataSourceCustomHeightCompletionHandle)customHeightCompletionHandle;
94-
9585
/**
9686
点击 Cell 的回调
9787
@@ -100,26 +90,6 @@ typedef CGFloat (^ZHCollectionViewDataSourceCustomHeightCompletionHandle)(ZHColl
10090
*/
10191
+ (void)didSelectRowAtWithDataSource:(ZHCollectionViewDataSource *)dataSource indexPath:(NSIndexPath *)indexPath;
10292

103-
/**
104-
获取 Header 的高度
105-
106-
@param dataSource ZHCollectionViewDataSource数据源
107-
@param section 分组索引
108-
@param customHeightCompletionHandle 自定义高度回调
109-
@return 分组 Header 高度
110-
*/
111-
+ (CGFloat)heightForHeaderInSectionWithDataSource:(ZHCollectionViewDataSource *)dataSource section:(NSInteger)section customHeightCompletionHandle:(ZHCollectionViewDataSourceCustomHeightCompletionHandle)customHeightCompletionHandle;
112-
113-
/**
114-
获取 Footer 的高度
115-
116-
@param dataSource ZHCollectionViewDataSource数据源
117-
@param section 分组索引
118-
@param customHeightCompletionHandle 自定义高度回调
119-
@return 分组 Footer 高度
120-
*/
121-
+ (CGFloat)heightForFooterInSectionWithDataSource:(ZHCollectionViewDataSource *)dataSource section:(NSInteger)section customHeightCompletionHandle:(ZHCollectionViewDataSourceCustomHeightCompletionHandle)customHeightCompletionHandle;
122-
12393
/**
12494
获取Header
12595
@@ -148,5 +118,9 @@ typedef CGFloat (^ZHCollectionViewDataSourceCustomHeightCompletionHandle)(ZHColl
148118

149119
+ (CGSize)referenceSizeForHeaderFooterWithDataSource:(ZHCollectionViewDataSource *)dataSource style:(ZHCollectionViewHeaderFooterStyle)style section:(NSUInteger)section;
150120

121+
@property (nonatomic, copy) void (^scrollViewDidEndDraggingWillDecelerate)(UIScrollView *scrollView, BOOL willDecelerate);
122+
@property (nonatomic, copy) void (^scrollViewDidEndDecelerating)(UIScrollView *scrollView);
123+
@property (nonatomic, copy) void (^scrollViewDidEndScrollingAnimation)(UIScrollView *scrollView);
124+
151125
@end
152126

ZHTableViewGroup/Classes/ZHCollectionViewDataSource.m

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@interface ZHCollectionViewDataSource ()
1313

1414
@property (nonatomic, strong) NSMutableArray<ZHCollectionViewGroup *> *groups;
15-
@property (nonatomic, strong) UICollectionView *collectionView;
15+
@property (nonatomic, weak) UICollectionView *collectionView;
1616
@property (nonatomic, strong) ZHAutoConfigurationCollectionViewDelegate *autoConfiguration;
1717

1818
@end
@@ -21,22 +21,18 @@ @implementation ZHCollectionViewDataSource {
2121

2222
}
2323

24+
- (void)dealloc {
25+
NSLog(@"ZHCollectionViewDataSource 释放了");
26+
}
27+
2428
- (instancetype)initWithCollectionView:(UICollectionView *)CollectionView {
2529
if (self = [super init]) {
2630
_collectionView = CollectionView;
27-
self.autoConfigurationCollectionViewDelegate = YES;
31+
_autoConfigurationCollectionViewDelegate = YES;
2832
}
2933
return self;
3034
}
3135

32-
- (void)setAutoConfigurationCollectionViewDelegate:(BOOL)autoConfigurationCollectionViewDelegate {
33-
_autoConfigurationCollectionViewDelegate = autoConfigurationCollectionViewDelegate;
34-
if (autoConfigurationCollectionViewDelegate) {
35-
_collectionView.dataSource = self.autoConfiguration;
36-
_collectionView.delegate = self.autoConfiguration;
37-
}
38-
}
39-
4036
- (void)addGroupWithCompletionHandle:(ZHCollectionViewDataSourceAddGroupCompletionHandle)completionHandle {
4137
ZHCollectionViewGroup *group = [[ZHCollectionViewGroup alloc] init];
4238
if (completionHandle) {
@@ -46,9 +42,14 @@ - (void)addGroupWithCompletionHandle:(ZHCollectionViewDataSourceAddGroupCompleti
4642
}
4743

4844
- (void)reloadCollectionViewData {
45+
if (!self.collectionView.dataSource && self.isAutoConfigurationCollectionViewDelegate) {
46+
self.collectionView.dataSource = self.autoConfiguration;
47+
}
48+
if (!self.collectionView.delegate && self.isAutoConfigurationCollectionViewDelegate) {
49+
self.collectionView.delegate = self.autoConfiguration;
50+
}
4951
[self registerClasss];
5052
[self.collectionView reloadData];
51-
5253
}
5354

5455
+ (NSInteger)numberOfRowsInSectionWithDataSource:(ZHCollectionViewDataSource *)dataSource

ZHTableViewGroup/Classes/ZHCollectionViewGroup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef void(^ZHCollectionViewGroupAddHeaderFooterCompletionHandle)(ZHCollection
1919
@property (nonatomic, strong) ZHCollectionViewHeaderFooter *footer;
2020
@property (nonatomic, assign, readonly) NSInteger cellCount;
2121
@property (nonatomic, strong, readonly) NSMutableArray<ZHCollectionViewCell *> *cells;
22+
@property (nonatomic, assign) UIEdgeInsets sectionEdgeInsets;
2223

2324
- (void)registerHeaderFooterCellWithCollectionView:(UICollectionView *)CollectionView;
2425

ZHTableViewGroup/Classes/ZHCollectionViewGroup.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ @interface ZHCollectionViewGroup ()
1616

1717
@implementation ZHCollectionViewGroup
1818

19+
- (instancetype)init {
20+
if (self = [super init]) {
21+
_sectionEdgeInsets = UIEdgeInsetsZero;
22+
}
23+
return self;
24+
}
25+
1926
- (void)registerHeaderFooterCellWithCollectionView:(UICollectionView *)collectionView {
2027
if (self.header && self.header.identifier) {
2128
[collectionView registerClass:self.header.anyClass forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:self.header.identifier];

ZHTableViewGroup/Classes/ZHTableViewDataSource.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ typedef CGFloat (^ZHTableViewDataSourceCustomHeightCompletionHandle)(ZHTableView
3838
* 是否开启即将展示 复制数据 默认为 NO
3939
*/
4040
@property (nonatomic, assign) BOOL isWillDisplayData;
41+
/**
42+
添加优先级 如果 YES 启用新的优先级 先判断设置的高度 没有获取自定义 Block 的高度 其次是自动计算高度
43+
*/
44+
@property (nonatomic, assign) BOOL priorityHeight;
4145

4246
@property (nonatomic, strong, readonly) NSMutableArray<ZHTableViewGroup *> *groups;
4347

@@ -153,6 +157,9 @@ typedef CGFloat (^ZHTableViewDataSourceCustomHeightCompletionHandle)(ZHTableView
153157

154158
+ (void)dataSource:(ZHTableViewDataSource *)dataSource willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
155159

160+
@property (nonatomic, copy) void (^heightForRowAtIndexPath)(UITableView *tableView, NSIndexPath *indexPath, CGFloat height);
161+
@property (nonatomic, copy) void (^heightForHeaderInSection)(UITableView *tableView, NSUInteger section, CGFloat height);
162+
@property (nonatomic, copy) void (^heightForFooterInSection)(UITableView *tableView, NSUInteger section, CGFloat height);
156163
#pragma mark - Delegate Block
157164
/* UITableView 滑动, scrollViewDidScroll的代理 */
158165
@property (nonatomic, copy) void (^scrollViewDidScrollCompletionHandle)(UIScrollView *scrollView);

ZHTableViewGroup/Classes/ZHTableViewDataSource.m

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@interface ZHTableViewDataSource ()
1313

1414
@property (nonatomic, strong) NSMutableArray<ZHTableViewGroup *> *groups;
15-
@property (nonatomic, strong) UITableView *tableView;
15+
@property (nonatomic, weak) UITableView *tableView;
1616
@property (nonatomic, strong) ZHAutoConfigurationTableViewDelegate *autoConfiguration;
1717

1818
@end
@@ -93,25 +93,51 @@ + (CGFloat)heightForRowAtDataSource:(ZHTableViewDataSource *)dataSource
9393
return 0;
9494
}
9595

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;
96+
if (!dataSource.priorityHeight) {
97+
CGFloat automaticHeight = ({
98+
automaticHeight = CGFLOAT_MAX;
99+
if (cell.height == NSNotFound) {
100+
UITableViewCell *automaticHeightCell = [self cellForRowAtWithDataSource:dataSource indexPath:indexPath];
101+
NSIndexPath *realyIndexPath = [self indexPathWithDataSource:dataSource
102+
indexPath:indexPath];
103+
[cell configCellWithCell:automaticHeightCell
104+
indexPath:realyIndexPath];
105+
automaticHeight = [automaticHeightCell sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX)].height;
106+
}
107+
automaticHeight;
108+
});
109+
CGFloat height = cell.height;
110+
if (cell.height == NSNotFound && automaticHeight != CGFLOAT_MAX) {
111+
height = automaticHeight;
105112
}
106-
automaticHeight;
107-
});
108-
CGFloat height = cell.height;
109-
if (cell.height == NSNotFound && automaticHeight != CGFLOAT_MAX) {
110-
height = automaticHeight;
113+
return [self heightWithCustomHandle:height
114+
customCompletionHandle:customHeightCompletionHandle
115+
baseModel:cell];
116+
} else {
117+
CGFloat heigh = cell.height;
118+
if (heigh > 0 && heigh < CGFLOAT_MAX && heigh < NSNotFound) {
119+
return heigh;
120+
}
121+
if (customHeightCompletionHandle) {
122+
heigh = customHeightCompletionHandle(cell);
123+
if (heigh > 0 && heigh < CGFLOAT_MAX && heigh < NSNotFound) {
124+
return heigh;
125+
}
126+
}
127+
CGFloat automaticHeight = ({
128+
automaticHeight = CGFLOAT_MAX;
129+
if (cell.height == NSNotFound) {
130+
UITableViewCell *automaticHeightCell = [self cellForRowAtWithDataSource:dataSource indexPath:indexPath];
131+
NSIndexPath *realyIndexPath = [self indexPathWithDataSource:dataSource
132+
indexPath:indexPath];
133+
[cell configCellWithCell:automaticHeightCell
134+
indexPath:realyIndexPath];
135+
automaticHeight = [automaticHeightCell sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX)].height;
136+
}
137+
automaticHeight;
138+
});
139+
return automaticHeight;
111140
}
112-
return [self heightWithCustomHandle:height
113-
customCompletionHandle:customHeightCompletionHandle
114-
baseModel:cell];
115141
}
116142

117143
+ (void)didSelectRowAtWithDataSource:(ZHTableViewDataSource *)dataSource

0 commit comments

Comments
 (0)