1212@interface ZHCollectionViewDataSource ()
1313
1414@property (nonatomic , strong ) NSMutableArray <ZHCollectionViewGroup *> *groups;
15- @property (nonatomic , strong ) UICollectionView *CollectionView ;
15+ @property (nonatomic , strong ) UICollectionView *collectionView ;
1616@property (nonatomic , strong ) ZHAutoConfigurationCollectionViewDelegate *autoConfiguration;
1717
1818@end
1919
20- @implementation ZHCollectionViewDataSource
20+ @implementation ZHCollectionViewDataSource {
21+
22+ }
2123
2224- (instancetype )initWithCollectionView : (UICollectionView *)CollectionView {
2325 if (self = [super init ]) {
24- _CollectionView = CollectionView;
26+ _collectionView = CollectionView;
2527 self.autoConfigurationCollectionViewDelegate = YES ;
2628 }
2729 return self;
@@ -30,8 +32,8 @@ - (instancetype)initWithCollectionView:(UICollectionView *)CollectionView {
3032- (void )setAutoConfigurationCollectionViewDelegate : (BOOL )autoConfigurationCollectionViewDelegate {
3133 _autoConfigurationCollectionViewDelegate = autoConfigurationCollectionViewDelegate;
3234 if (autoConfigurationCollectionViewDelegate) {
33- _CollectionView .dataSource = self.autoConfiguration ;
34- _CollectionView .delegate = self.autoConfiguration ;
35+ _collectionView .dataSource = self.autoConfiguration ;
36+ _collectionView .delegate = self.autoConfiguration ;
3537 }
3638}
3739
@@ -45,7 +47,7 @@ - (void)addGroupWithCompletionHandle:(ZHCollectionViewDataSourceAddGroupCompleti
4547
4648- (void )reloadCollectionViewData {
4749 [self registerClasss ];
48- [self .CollectionView reloadData ];
50+ [self .collectionView reloadData ];
4951}
5052
5153+ (NSInteger )numberOfRowsInSectionWithDataSource : (ZHCollectionViewDataSource *)dataSource
@@ -64,7 +66,7 @@ + (UICollectionViewCell *)cellForRowAtWithDataSource:(ZHCollectionViewDataSource
6466 if (!group) {
6567 return cell;
6668 }
67- UICollectionViewCell *resultCell = [group cellForCollectionViewWithCollectionView: dataSource.CollectionView indexPath: indexPath];
69+ UICollectionViewCell *resultCell = [group cellForCollectionViewWithCollectionView: dataSource.collectionView indexPath: indexPath];
6870 if (!resultCell) {
6971 return cell;
7072 }
@@ -78,20 +80,6 @@ + (NSInteger)numberOfSectionsWithDataSource:(ZHCollectionViewDataSource *)dataSo
7880 return dataSource.groups .count ;
7981}
8082
81- + (CGFloat)heightForRowAtDataSource : (ZHCollectionViewDataSource *)dataSource
82- indexPath : (NSIndexPath *)indexPath customHeightCompletionHandle : (ZHCollectionViewDataSourceCustomHeightCompletionHandle)customHeightCompletionHandle {
83- ZHCollectionViewCell *cell = [self cellForIndexPath: dataSource indexPath: indexPath];
84- if (!cell) {
85- return 0 ;
86- }
87- UICollectionViewCell *automaticHeightCell = [self cellForRowAtWithDataSource: dataSource indexPath: indexPath];
88- CGFloat automaticHeight = [automaticHeightCell sizeThatFits: CGSizeMake ([UIScreen mainScreen ].bounds.size.width, CGFLOAT_MAX)].height ;
89- if (cell.height == NSNotFound && automaticHeight != CGFLOAT_MAX) {
90- cell.height = automaticHeight;
91- }
92- return [self heightWithCustomHandle: cell.height customCompletionHandle: customHeightCompletionHandle baseModel: cell];
93- }
94-
9583+ (void )didSelectRowAtWithDataSource : (ZHCollectionViewDataSource *)dataSource
9684 indexPath : (NSIndexPath *)indexPath {
9785 ZHCollectionViewCell *CollectionViewCell = [self cellForIndexPath: dataSource indexPath: indexPath];
@@ -103,16 +91,6 @@ + (void)didSelectRowAtWithDataSource:(ZHCollectionViewDataSource *)dataSource
10391 [CollectionViewCell didSelectRowAtWithCell: cell indexPath: [group indexPathWithCell: CollectionViewCell indexPath: indexPath]];
10492}
10593
106- + (CGFloat)heightForHeaderInSectionWithDataSource : (ZHCollectionViewDataSource *)dataSource
107- section : (NSInteger )section customHeightCompletionHandle : (ZHCollectionViewDataSourceCustomHeightCompletionHandle)customHeightCompletionHandle {
108- return [self heightForHeaderFooterInSectionWithDataSource: dataSource section: section style: ZHCollectionViewHeaderFooterStyleHeader customHeightCompletionHandle: customHeightCompletionHandle];
109- }
110-
111- + (CGFloat)heightForFooterInSectionWithDataSource : (ZHCollectionViewDataSource *)dataSource
112- section : (NSInteger )section customHeightCompletionHandle : (ZHCollectionViewDataSourceCustomHeightCompletionHandle)customHeightCompletionHandle {
113- return [self heightForHeaderFooterInSectionWithDataSource: dataSource section: section style: ZHCollectionViewHeaderFooterStyleFooter customHeightCompletionHandle: customHeightCompletionHandle];
114- }
115-
11694+ (UICollectionReusableView *)viewForHeaderInSectionWithDataSource : (ZHCollectionViewDataSource *)dataSource
11795 section : (NSInteger )section {
11896 return [self viewHeaderFooterInSectionWithDtaSource: dataSource section: section style: ZHCollectionViewHeaderFooterStyleHeader];
@@ -133,37 +111,7 @@ + (UICollectionReusableView *)viewHeaderFooterInSectionWithDtaSource:(ZHCollecti
133111 if (!group) {
134112 return nil ;
135113 }
136- return [group headerFooterForStyle: style CollectionView: dataSource.CollectionView section: section];
137- }
138-
139- + (CGFloat)heightForHeaderFooterInSectionWithDataSource : (ZHCollectionViewDataSource *)dataSource
140- section : (NSInteger )section style : (ZHCollectionViewHeaderFooterStyle)style
141- customHeightCompletionHandle : (ZHCollectionViewDataSourceCustomHeightCompletionHandle)customHeightCompletionHandle {
142- ZHCollectionViewGroup *group = [self groupForSectionWithDataSource: dataSource section: section];
143- if (!group) {
144- return 0 ;
145- }
146- NSInteger height = 0 ;
147- ZHCollectionViewBaseModel *baseModel;
148- UICollectionReusableView *headFooter = [self viewHeaderFooterInSectionWithDtaSource: dataSource section: section style: style];
149- CGFloat automaticHeight = [headFooter sizeThatFits: CGSizeMake ([UIScreen mainScreen ].bounds.size.width, CGFLOAT_MAX)].height ;
150- switch (style) {
151- case ZHCollectionViewHeaderFooterStyleHeader: {
152- height = group.header .height ;
153- baseModel = group.header ;
154-
155- }
156- break ;
157- case ZHCollectionViewHeaderFooterStyleFooter: {
158- height = group.footer .height ;
159- baseModel = group.footer ;
160- }
161- break ;
162- }
163- if (height == NSNotFound && automaticHeight != CGFLOAT_MAX) {
164- height = automaticHeight;
165- }
166- return [self heightWithCustomHandle: height customCompletionHandle: customHeightCompletionHandle baseModel: baseModel];
114+ return [group headerFooterForStyle: style collectionView: dataSource.collectionView section: section];
167115}
168116
169117+ (CGFloat)heightWithCustomHandle : (CGFloat)height
@@ -205,9 +153,39 @@ + (NSIndexPath *)indexPathWithDataSource:(ZHCollectionViewDataSource *)dataSourc
205153 return [group indexPathWithCell: CollectionViewCell indexPath: indexPath];
206154}
207155
156+ + (CGSize)sizeForItemWithDataSource : (ZHCollectionViewDataSource *)dataSource indexPath : (NSIndexPath *)indexPath {
157+ ZHCollectionViewCell *cell = [self cellForIndexPath: dataSource indexPath: indexPath];
158+ if (!CGSizeEqualToSize (cell.size , CGSizeZero)) {
159+ return cell.size ;
160+ }
161+ UICollectionViewCell *collectionViewCell = [dataSource collectionViewWithClass: cell.anyClass];
162+ [cell configCellWithCell: collectionViewCell indexPath: indexPath];
163+ return [collectionViewCell sizeThatFits: [dataSource sizeFit ]];
164+ }
165+
166+ + (CGSize)referenceSizeForHeaderFooterWithDataSource : (ZHCollectionViewDataSource *)dataSource style : (ZHCollectionViewHeaderFooterStyle)style section : (NSUInteger )section {
167+ ZHCollectionViewGroup *group = [self groupForSectionWithDataSource: dataSource section: section];
168+ ZHCollectionViewHeaderFooter *headerFooter = style == ZHCollectionViewHeaderFooterStyleHeader ? group.header : group.footer ;
169+ if (!CGSizeEqualToSize (headerFooter.size , CGSizeZero)) {
170+ return headerFooter.size ;
171+ }
172+ UICollectionReusableView *view = [[headerFooter.anyClass alloc ] initWithFrame: CGRectZero];
173+ [headerFooter setHeaderFooter: view section: section];
174+ return [view sizeThatFits: [dataSource sizeFit ]];
175+ }
176+
177+ - (UICollectionViewCell *)collectionViewWithClass : (Class )class {
178+ UICollectionViewCell *cell = [[class alloc ] initWithFrame: CGRectZero];
179+ return cell;
180+ }
181+
182+ - (CGSize)sizeFit {
183+ return CGSizeMake (CGRectGetWidth (_collectionView.frame ), CGFLOAT_MAX);
184+ }
185+
208186- (void )registerClasss {
209187 for (ZHCollectionViewGroup *group in self.groups ) {
210- [group registerHeaderFooterCellWithCollectionView: self .CollectionView ];
188+ [group registerHeaderFooterCellWithCollectionView: self .collectionView ];
211189 }
212190}
213191
0 commit comments