Skip to content

Commit 7edf26b

Browse files
committed
新增 UICollectionView 托管类
1 parent 7498d57 commit 7edf26b

14 files changed

Lines changed: 1033 additions & 10 deletions

.DS_Store

0 Bytes
Binary file not shown.

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 55 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// ZHAutoConfigurationCollectionViewDelegate.h
3+
// Pods
4+
//
5+
// Created by 张行 on 2017/4/20.
6+
//
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
typedef NS_ENUM(NSUInteger, ZHCollectionViewCustomHeightType) {
12+
ZHCollectionViewCustomHeightTypeCell,
13+
ZHCollectionViewCustomHeightTypeHeader,
14+
ZHCollectionViewCustomHeightTypeFooter
15+
};
16+
17+
@class ZHCollectionViewDataSource;
18+
19+
/**
20+
自动配置 UICollectionView 的数据源和代理
21+
默认实现 UICollectionView 的数据源方法和代理方法如下
22+
*/
23+
@interface ZHAutoConfigurationCollectionViewDelegate : NSObject <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
24+
25+
/**
26+
初始化自动配置ZHAutoConfigurationCollectionViewDelegate对象
27+
28+
@param dataSource ZHCollectionViewDataSource
29+
@return ZHAutoConfigurationCollectionViewDelegate
30+
*/
31+
- (instancetype)initWithDataSource:(ZHCollectionViewDataSource *)dataSource;
32+
33+
@end
34+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// ZHAutoConfigurationCollectionViewDelegate.m
3+
// Pods
4+
//
5+
// Created by 张行 on 2017/4/20.
6+
//
7+
//
8+
9+
#import "ZHAutoConfigurationCollectionViewDelegate.h"
10+
#import "ZHCollectionViewDataSource.h"
11+
12+
@implementation ZHAutoConfigurationCollectionViewDelegate {
13+
ZHCollectionViewDataSource *_dataSource;
14+
}
15+
16+
- (instancetype)initWithDataSource:(ZHCollectionViewDataSource *)dataSource {
17+
if (self = [super init]) {
18+
_dataSource = dataSource;
19+
}
20+
return self;
21+
}
22+
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+
33+
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
34+
return [ZHCollectionViewDataSource numberOfSectionsWithDataSource:_dataSource];
35+
}
36+
37+
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
38+
return [ZHCollectionViewDataSource cellForRowAtWithDataSource:_dataSource indexPath:indexPath];
39+
}
40+
41+
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
42+
return [ZHCollectionViewDataSource numberOfRowsInSectionWithDataSource:_dataSource section:section];
43+
}
44+
45+
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
46+
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
47+
return [ZHCollectionViewDataSource viewForHeaderInSectionWithDataSource:_dataSource section:indexPath.section];
48+
} else {
49+
return [ZHCollectionViewDataSource viewForFooterInSectionWithDataSource:_dataSource section:indexPath.section];
50+
}
51+
}
52+
53+
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
54+
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
55+
[ZHCollectionViewDataSource didSelectRowAtWithDataSource:_dataSource indexPath:indexPath];
56+
}
57+
58+
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
59+
return CGSizeZero;
60+
}
61+
62+
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
63+
return CGSizeZero;
64+
}
65+
66+
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
67+
return CGSizeZero;
68+
}
69+
70+
@end
71+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// ZHCollectionViewBaseModel.h
3+
// Pods
4+
//
5+
// Created by 张行 on 2018/2/6.
6+
//
7+
//
8+
9+
@class ZHCollectionViewBaseModel;
10+
11+
typedef CGFloat (^ZHCollectionViewBaseModelCustomHeightCompletionHandle)(UICollectionView *collectionView, NSIndexPath *indexPath, ZHCollectionViewBaseModel *model);
12+
13+
/**
14+
配置 Cell Header Footer 的数据 Model
15+
*/
16+
@interface ZHCollectionViewBaseModel : NSObject
17+
18+
/**
19+
标识符
20+
*/
21+
@property (nonatomic, copy) NSString *identifier;
22+
/**
23+
自定义类 Class
24+
*/
25+
@property (nonatomic, strong) Class anyClass;
26+
/**
27+
高度 默认为 NSNotFound
28+
*/
29+
@property (nonatomic, assign) CGFloat height;
30+
/**
31+
* 自定义高度
32+
*/
33+
@property (nonatomic, copy) ZHCollectionViewBaseModelCustomHeightCompletionHandle customHeightCompletionHandle;
34+
35+
@end
36+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// ZHCollectionViewBaseModel.m
3+
// Pods
4+
//
5+
// Created by 张行 on 2018/2/6.
6+
//
7+
//
8+
9+
#import "ZHCollectionViewBaseModel.h"
10+
11+
@implementation ZHCollectionViewBaseModel
12+
13+
- (instancetype)init {
14+
if (self = [super init]) {
15+
_height = NSNotFound;
16+
}
17+
return self;
18+
}
19+
20+
@end
21+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
//
3+
// ZHCollectionViewCell.h
4+
// Pods
5+
//
6+
// Created by 张行 on 2018/2/6.
7+
//
8+
//
9+
10+
#import "ZHCollectionViewBaseModel.h"
11+
12+
/**
13+
注册 Cell 样式
14+
*/
15+
@interface ZHCollectionViewCell<CellType:UICollectionViewCell *> : ZHCollectionViewBaseModel
16+
17+
/**
18+
Cell 的个数 默认为1
19+
*/
20+
@property (nonatomic, assign) NSInteger cellNumber;
21+
/**
22+
配置 Cell的回调
23+
*/
24+
@property (nonatomic, copy) void(^configCompletionHandle)(CellType cell, NSIndexPath *indexPath);
25+
/**
26+
点击 Cell 的回调
27+
*/
28+
@property (nonatomic, copy) void(^didSelectRowCompletionHandle)(CellType cell, NSIndexPath *indexPath);
29+
30+
/**
31+
* 为了是支持泛型
32+
33+
@param configCompletionHandle 设置的Block
34+
*/
35+
- (void)setConfigCompletionHandle:(void (^)(CellType cell, NSIndexPath * indexPath))configCompletionHandle;
36+
37+
/**
38+
* 为了支持泛型
39+
40+
@param didSelectRowCompletionHandle 点击回调的block
41+
*/
42+
- (void)setDidSelectRowCompletionHandle:(void (^)(CellType cell, NSIndexPath * indexPath))didSelectRowCompletionHandle;
43+
44+
/**
45+
点击所在的 Cell 的执行方法
46+
47+
@param cell 点击的 Cell
48+
@param indexPath 点击 cell 所在的索引
49+
*/
50+
- (void)didSelectRowAtWithCell:(CellType)cell
51+
indexPath:(NSIndexPath *)indexPath;
52+
53+
/**
54+
配置 Cell 的执行方法
55+
56+
@param cell 配置的 Cell
57+
@param indexPath 配置 Cell 所在的索引
58+
*/
59+
- (void)configCellWithCell:(CellType)cell
60+
indexPath:(NSIndexPath *)indexPath;
61+
62+
/**
63+
一个方法配置所有的参数
64+
65+
@param cellNumber cell 的数量
66+
@param identifier 标识符
67+
@param anyClass cell 的类名
68+
@param height 高度
69+
@param configCompletionHandle 配置 cell
70+
@param didSelectRowCompletionHandle 点击 cell 方法
71+
*/
72+
- (void)configurationCellWithCellNumber:(NSUInteger)cellNumber
73+
identifier:(NSString *)identifier
74+
anyClass:(Class)anyClass
75+
height:(CGFloat)height
76+
configCompletionHandle:(void(^)(CellType cell, NSIndexPath *indexPath))configCompletionHandle
77+
didSelectRowCompletionHandle:(void(^)(CellType cell, NSIndexPath *indexPath))didSelectRowCompletionHandle;
78+
79+
@end
80+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// ZHCollectionViewCell.m
3+
// Pods
4+
//
5+
// Created by 张行 on 2018/2/6.
6+
//
7+
//
8+
9+
#import "ZHCollectionViewCell.h"
10+
11+
@implementation ZHCollectionViewCell {
12+
}
13+
14+
- (instancetype)init {
15+
if (self = [super init]) {
16+
_cellNumber = 1;
17+
}
18+
return self;
19+
}
20+
21+
- (void)setConfigCompletionHandle:(void (^)(UICollectionViewCell *, NSIndexPath *))configCompletionHandle {
22+
_configCompletionHandle = configCompletionHandle;
23+
}
24+
25+
- (void)setDidSelectRowCompletionHandle:(void (^)(UICollectionViewCell *, NSIndexPath *))didSelectRowCompletionHandle {
26+
_didSelectRowCompletionHandle = didSelectRowCompletionHandle;
27+
}
28+
29+
- (void)didSelectRowAtWithCell:(UICollectionViewCell *)cell
30+
indexPath:(NSIndexPath *)indexPath {
31+
if (!self.didSelectRowCompletionHandle) {
32+
return;
33+
}
34+
self.didSelectRowCompletionHandle(cell,indexPath);
35+
}
36+
37+
- (void)configCellWithCell:(UICollectionViewCell *)cell
38+
indexPath:(NSIndexPath *)indexPath {
39+
if (!self.configCompletionHandle) {
40+
return;
41+
}
42+
self.configCompletionHandle(cell,indexPath);
43+
}
44+
45+
- (void)configurationCellWithCellNumber:(NSUInteger)cellNumber
46+
identifier:(NSString *)identifier
47+
anyClass:(Class)anyClass
48+
height:(CGFloat)height
49+
configCompletionHandle:(void (^)(UICollectionViewCell *, NSIndexPath *))configCompletionHandle
50+
didSelectRowCompletionHandle:(void (^)(UICollectionViewCell *, NSIndexPath *))didSelectRowCompletionHandle {
51+
self.cellNumber = cellNumber;
52+
self.identifier = identifier;
53+
self.anyClass = anyClass;
54+
self.height = height;
55+
self.configCompletionHandle = configCompletionHandle;
56+
self.didSelectRowCompletionHandle = didSelectRowCompletionHandle;
57+
}
58+
59+
@end
60+

0 commit comments

Comments
 (0)