WFDataSource
基于块的 UITableView/UICollectionView 数据源
特性
- 整洁的 Table View 和 Collection View 代码
- 使用 block 将 tableView 的数据源和代理方法迁移到 viewDidLoad: 中
- 单个数组可包含多个不同类别的 cell
- 同时支持 XIB 和纯代码创建的 cell
用法
//cells for cell models
NSDictionary *modelCellMap = @{ @"DemoCellModel":@"DemoCell", @"DemoCellModel_XIB":@"DemoCell_XIB" };
//create a data source
WFDataSource *dataSource = [[WFDataSource alloc] initWithModelCellMap:modelCellMap cellConfigBlock:^(id cell, id item, NSIndexPath *indexPath) {
[cell configWithItem:item];
}];
//on select cell
dataSource.didSelectCellBlock = ^(id item, NSIndexPath *indexPath) {
NSLog(@"%@ - section %@, row %@",item, @(indexPath.section), @(indexPath.row));
};
//cell height
dataSource.heightForRow = ^CGFloat (id item, NSIndexPath *indexPath) {
return 44;
};
//section header view
dataSource.headerViewForSection = ^ UIView *(id sectionItem, NSInteger section){
WFDataSourceSection *sectionData = (WFDataSourceSection *)sectionItem;
if (sectionData.sectionTitle) {
UIView *bg = [UIView new];
bg.frame = CGRectMake(0, 0, 0, 30);
bg.backgroundColor = [UIColor lightGrayColor];
UILabel *label = [UILabel new];
label.frame = CGRectMake(10, 0, 100, 30);
label.text = sectionData.sectionTitle;
[bg addSubview:label];
return bg;
}
return nil;
};
self.dataSource.tableView = self.tableView;
//reload cell with model array
[self.dataSource reloadWithSectionItems:[self sectionModels]];
//or
[self.dataSource reloadWithItems:[self models]];
中文文档
基于 block 的 TableView/CollectionView 数据源和代理方法的封装
特性
- 使 Table View & Collection 代码更简洁
- 使用 block 将 tableView 的数据源和代理方法精简到 viewDidLoad: 中
- 单个数组可以包含多个不同类型的 cell
- 支持使用 XIB 创建和纯代码创建的 cell
简介
在iOS界面开发中,每个控制器中最频繁出现的代码可能是TableView的相关方法。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
上述代码在项目中频繁出现,TableView通过这些数据源方法和代理方法在view controllers之间传递信息,几乎所有的工作都在view controllers中完成。为了避免所有工作都由view controllers完成,我们可以将所有这些繁杂的任务交给一个专门的类来处理,而view controllers只需要在viewDidLoad:中告诉这个类要做什么。基于这一思路,我对TableView/CollectionView的数据源和代理方法进行了封装。
MVC模式下,每个Cell应该有一个对应的Model来处理数据业务,在初始化WFDataSource时,需要传入Model与Cell的对应关系。通过block回调,将cell与model对应起来。
//cells for cell models
NSDictionary *modelCellMap = @{ @"DemoCellModel":@"DemoCell", @"DemoCellModel_XIB":@"DemoCell_XIB" };
//create a data source
WFDataSource *dataSource = [[WFDataSource alloc] initWithModelCellMap:modelCellMap cellConfigBlock:^(id cell, id item, NSIndexPath *indexPath) {
[cell configWithItem:item];
}];
在项目中往往会出现使用XIB创建的Cell和纯代码Cell混合使用的情况,在通过table view的缓存池机制创建cell时,两者之间的差异可以通过下面两个方法进行统一。
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier;
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier;
WFDataSource对此进行了处理,传入的cell支持任意方式创建,并可以混用。TableView的其他数据源方法和代理方法,通过block的方式扁平化处理。
//on select cell
dataSource.didSelectCellBlock = ^(id item, NSIndexPath *indexPath) {
NSLog(@"%@ - section %@, row %@",item, @(indexPath.section), @(indexPath.row));
};
//cell height
dataSource.heightForRow = ^CGFloat (id item, NSIndexPath *indexPath) {
return 44;
};
//section header view
dataSource.headerViewForSection = ^ UIView *(id sectionItem, NSInteger section){
WFDataSourceSection *sectionData = (WFDataSourceSection *)sectionItem;
if (sectionData.sectionTitle) {
UIView *bg = [UIView new];
bg.frame = CGRectMake(0, 0, 0, 30);
bg.backgroundColor = [UIColor lightGrayColor];
UILabel *label = [UILabel new];
label.frame = CGRectMake(10, 0, 100, 30);
label.text = sectionData.sectionTitle;
[bg addSubview:label];
return bg;
}
return nil;
};
dataSource创建后,需要将其与绑定的table view绑定。并刷新cell。
self.dataSource.tableView = self.tableView;
//reload cell with model array
[self.dataSource reloadWithSectionItems:[self sectionModels]];
//or
[self.dataSource reloadWithItems:[self models]];