Skip to content

Commit c1f6291

Browse files
author
zhanghang
committed
修复 UICollectionView 托管类的问题
1 parent 116aead commit c1f6291

11 files changed

Lines changed: 67 additions & 86 deletions

ZHTableViewGroup/Classes/ZHAutoConfigurationCollectionViewDelegate.m

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ - (instancetype)initWithDataSource:(ZHCollectionViewDataSource *)dataSource {
2020
return self;
2121
}
2222

23-
- (ZHCollectionViewDataSourceCustomHeightCompletionHandle)completionHandleWithCollectionView:(UICollectionView *)collectionView heightAtIndexPath:(NSIndexPath *)indexPath {
24-
ZHCollectionViewDataSourceCustomHeightCompletionHandle completionHandle = ^CGFloat(ZHCollectionViewBaseModel *model) {
25-
if (!model.customHeightCompletionHandle) {
26-
return model.height;
27-
}
28-
return model.customHeightCompletionHandle(collectionView,[ZHCollectionViewDataSource indexPathWithDataSource:_dataSource indexPath:indexPath],model);
29-
};
30-
return completionHandle;
31-
}
32-
3323
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
3424
return [ZHCollectionViewDataSource numberOfSectionsWithDataSource:_dataSource];
3525
}
@@ -56,15 +46,15 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
5646
}
5747

5848
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
59-
return CGSizeZero;
49+
return [ZHCollectionViewDataSource sizeForItemWithDataSource:_dataSource indexPath:indexPath];
6050
}
6151

6252
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
63-
return CGSizeZero;
53+
return [ZHCollectionViewDataSource referenceSizeForHeaderFooterWithDataSource:_dataSource style:ZHCollectionViewHeaderFooterStyleHeader section:section];
6454
}
6555

6656
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
67-
return CGSizeZero;
57+
return [ZHCollectionViewDataSource referenceSizeForHeaderFooterWithDataSource:_dataSource style:ZHCollectionViewHeaderFooterStyleFooter section:section];
6858
}
6959

7060
@end

ZHTableViewGroup/Classes/ZHCollectionViewBaseModel.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ typedef CGFloat (^ZHCollectionViewBaseModelCustomHeightCompletionHandle)(UIColle
2525
@property (nonatomic, strong) Class anyClass;
2626
/**
2727
高度 默认为 NSNotFound
28+
height 属性废弃 用size
2829
*/
29-
@property (nonatomic, assign) CGFloat height;
30+
//@property (nonatomic, assign) CGFloat height;
31+
32+
33+
/**
34+
* 设置 UICollectionCell UICollectionReusableView 的大小
35+
* 默认 CGSizeZero
36+
*/
37+
@property (nonatomic, assign) CGSize size;
38+
3039
/**
3140
* 自定义高度
3241
*/

ZHTableViewGroup/Classes/ZHCollectionViewBaseModel.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ @implementation ZHCollectionViewBaseModel
1212

1313
- (instancetype)init {
1414
if (self = [super init]) {
15-
_height = NSNotFound;
15+
_size = CGSizeZero;
1616
}
1717
return self;
1818
}

ZHTableViewGroup/Classes/ZHCollectionViewCell.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
- (void)configurationCellWithCellNumber:(NSUInteger)cellNumber
7373
identifier:(NSString *)identifier
7474
anyClass:(Class)anyClass
75-
height:(CGFloat)height
75+
size:(CGSize)size
7676
configCompletionHandle:(void(^)(CellType cell, NSIndexPath *indexPath))configCompletionHandle
7777
didSelectRowCompletionHandle:(void(^)(CellType cell, NSIndexPath *indexPath))didSelectRowCompletionHandle;
7878

ZHTableViewGroup/Classes/ZHCollectionViewCell.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ - (void)configCellWithCell:(UICollectionViewCell *)cell
4545
- (void)configurationCellWithCellNumber:(NSUInteger)cellNumber
4646
identifier:(NSString *)identifier
4747
anyClass:(Class)anyClass
48-
height:(CGFloat)height
48+
size:(CGSize)size
4949
configCompletionHandle:(void (^)(UICollectionViewCell *, NSIndexPath *))configCompletionHandle
5050
didSelectRowCompletionHandle:(void (^)(UICollectionViewCell *, NSIndexPath *))didSelectRowCompletionHandle {
5151
self.cellNumber = cellNumber;
5252
self.identifier = identifier;
5353
self.anyClass = anyClass;
54-
self.height = height;
54+
self.size = size;
5555
self.configCompletionHandle = configCompletionHandle;
5656
self.didSelectRowCompletionHandle = didSelectRowCompletionHandle;
5757
}

ZHTableViewGroup/Classes/ZHCollectionViewDataSource.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,9 @@ typedef CGFloat (^ZHCollectionViewDataSourceCustomHeightCompletionHandle)(ZHColl
146146

147147
+ (NSIndexPath *)indexPathWithDataSource:(ZHCollectionViewDataSource *)dataSource indexPath:(NSIndexPath *)indexPath;
148148

149+
+ (CGSize)sizeForItemWithDataSource:(ZHCollectionViewDataSource *)dataSource indexPath:(NSIndexPath *)indexPath;
150+
151+
+ (CGSize)referenceSizeForHeaderFooterWithDataSource:(ZHCollectionViewDataSource *)dataSource style:(ZHCollectionViewHeaderFooterStyle)style section:(NSUInteger)section;
152+
149153
@end
150154

ZHTableViewGroup/Classes/ZHCollectionViewDataSource.m

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
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

ZHTableViewGroup/Classes/ZHCollectionViewGroup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef void(^ZHCollectionViewGroupAddHeaderFooterCompletionHandle)(ZHCollection
3131

3232
- (ZHCollectionViewCell *)CollectionViewCellForIndexPath:(NSIndexPath *)indexPath;
3333

34-
- (UICollectionReusableView *)headerFooterForStyle:(ZHCollectionViewHeaderFooterStyle)style CollectionView:(UICollectionView *)CollectionView section:(NSUInteger)section;
34+
- (UICollectionReusableView *)headerFooterForStyle:(ZHCollectionViewHeaderFooterStyle)style collectionView:(UICollectionView *)collectionView section:(NSUInteger)section;
3535

3636
- (NSIndexPath *)indexPathWithCell:(ZHCollectionViewCell *)cell indexPath:(NSIndexPath *)indexPath;
3737
@end

ZHTableViewGroup/Classes/ZHCollectionViewGroup.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ - (UICollectionReusableView *)headerFooterForStyle:(ZHCollectionViewHeaderFooter
124124
if (!headerFooter.identifier) {
125125
return nil;
126126
}
127-
UICollectionReusableView *headerFooterView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerFooterView.reuseIdentifier forIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
127+
UICollectionReusableView *headerFooterView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerFooter.identifier forIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
128128
[headerFooter setHeaderFooter:headerFooterView section:section];
129129
return headerFooterView;
130130
}

ZHTableViewGroup/Classes/ZHCollectionViewHeaderFooter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
#import <UIKit/UIKit.h>
10-
#import "ZHTableViewBaseModel.h"
10+
#import "ZHCollectionViewBaseModel.h"
1111

1212
/**
1313
* 配置UICollectionVieweaderFooterView的回调
@@ -22,7 +22,7 @@ typedef NS_ENUM(NSUInteger, ZHCollectionViewHeaderFooterStyle) {
2222
ZHCollectionViewHeaderFooterStyleFooter
2323
};
2424

25-
@interface ZHCollectionViewHeaderFooter<HeaderFooter:UICollectionReusableView *> : ZHTableViewBaseModel
25+
@interface ZHCollectionViewHeaderFooter<HeaderFooter:UICollectionReusableView *> : ZHCollectionViewBaseModel
2626

2727
@property (nonatomic, assign) ZHCollectionViewHeaderFooterStyle style;
2828

0 commit comments

Comments
 (0)