【iOS】一个轻量级的数据驱动列表框架 YHListKit

YHListKit 是一个基于  UICollectionView 的、轻量级的数据驱动列表框架,其核心思想在于通过 Adapter 模式将繁琐的 UICollectionView 相关代理方法转变成数据驱动的接口,更贴近人类的思维方式,同时还将注册 cell 和 dequeue cell 的逻辑封装到了内部,另外还通过借助消息转发机制,将 UICollectionViewDelegate、UIScrollViewDelegate 等代理方法由中间人转发出来,以供外面的使用方在需要时可以使用。

特性

基于 UICollectionView 的适配器,不需要再面对繁琐的 register -> data source -> dequeue 流程

真正的数据驱动

自动缓存 cell/section header/section footer 的高度

使用了面向协议的设计,去耦合

不需要继承,即插即用,无侵入性

架构

【iOS】一个轻量级的数据驱动列表框架 YHListKit_第1张图片

【iOS】一个轻量级的数据驱动列表框架 YHListKit_第2张图片

使用方法

1. 创建 collection view:

1
2
3
4
5
6
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
collectionViewLayout:self.collectionViewLayout];  // 这里也可以使用自己的 layout
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.collectionView.backgroundColor = [UIColor colorWithRed: 244   green: 244   blue: 244   alpha: 1.0 ];
self.collectionView.alwaysBounceVertical = YES;
[self.view addSubview:self.collectionView];

2. 创建 YHCollectionViewAdapter ,绑定 collectionView,设置代理:

1
2
3
4
self.adapter = [[YHCollectionViewAdapter alloc] init];
self.adapter.collectionView = self.collectionView;
self.adapter.collectionViewDelegate = self;
self.adapter.delegate = self;

3. 设置 view model 数据,也就是创建 section model 和 cell model,配置相关数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
NSMutableArray *sections = [NSMutableArray array];
for   ( int   section =  0 ; section <  4 ; section++) {
 
     // 创建 section model
     YHCollectionViewSectionModel *sectionModel = [[YHCollectionViewSectionModel alloc] init];
 
     NSMutableArray *rows = [NSMutableArray array];
     for   ( int   row =  0 ; row <  10 ; row++) {
 
         // 创建 cell model
         YHCollectionViewCellModel *cellModel = [[YHCollectionViewCellModel alloc] init];
         cellModel.dataModel = [NSString stringWithFormat:@ "%i - %i" , section, row];  // 设置 model 数据
         cellModel.cellClass = [SCCutomCollectionViewCell  class ];                     // 设置 cell class
         cellModel.cellHeight = [UIScreen mainScreen].bounds.size.width *  0.656 ;      // 设置 cell 高度,也可以在对应的 cell 中实现相应的协议方法来实现
 
         [rows addObject:cellModel];
     }
 
     sectionModel.cellModels = rows;  // 设置该 section 的 cell model 集合
     sectionModel.headerClass = [SCCollectionSectionHeaderView  class ];  // 设置 section header 的 class
     sectionModel.headerHeight =  50 ;                                    // 设置 section header 的 高度
     sectionModel.footerClass = [SCCollectionSectionFooterView  class ];  // 设置 section footer 的 class
     sectionModel.footerHeight =  20 ;                                    // 设置 section footer 的 高度
     [sections addObject:sectionModel];
}
 
self.adapter.sectionModels = sections;
 
[self.collectionView reloadData];

4. 除了在 view model 层设置 cell 、 section header 和 section footer 的高度之外,还可以在对应的 view 层设置高度,只需要实现 YHCollectionViewCell 和 YHCollectionViewSectionHeaderFooter 协议中定义的方法即可:

1
2
3
4
5
6
7
8
9
@protocol YHCollectionViewCell 
 
...
 
+ (CGFloat)cellHeightWithModel:(YHCollectionViewCellModel *)model;
+ (CGFloat)cellWidthWithModel:(YHCollectionViewCellModel *)model;
 
 
@end
1
2
3
4
5
6
7
8
@protocol YHCollectionViewSectionHeaderFooter 
 
...
 
+ (CGFloat)heightWithModel:(YHCollectionViewSectionModel *)model;
+ (CGFloat)widthWithModel:(YHCollectionViewSectionModel *)model;
 
@end

更详细的使用介绍见示例代码 Example。

系统要求

该项目最低支持 iOS 7.0。


你可能感兴趣的:(软件开发,iOS,iOS学习,iOS开发,iOS零基础,iOS入门,软件开发)