您的位置:首页 > 编程语言

编写整洁的TableView代码--基于Block的TableView/CollectionView数据源封装

2016-11-12 14:05 267 查看
iOS界面开发中,每个控制器中重复度最高的代码,可能就是 TableView 的相关方法了。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;


上面的代码在项目中大量出现,Table View 通过这些数据源方法和代理方法与 view controllers 之间传递信息,而几乎所有的任务都在 view controller 中进行。为了避免让 view controllers 做所有的事,我们可以将所有的这些繁琐的任务,交给一个专门的类来处理,view controllers 只需在 viewDidLoad: 中告诉这个类要做什么。基于这个思路,我对 Table View / Collection View 的数据源和代理方法进行了封装。

在MVC模式下,每个 Cell 应该有一个对应的 Model 来处理数据业务,在初始化 WFDataSource 时,需要传入Model与Cell的对应关系。通过block回调,将 cell 与 model 对应起来。

NSDictionary *modelCellMap = @{
@"DemoCellModel":@"DemoCell",
@"DemoCellModel_XIB":@"DemoCell_XIB",
};

WFDataSource *dataSource = [[WFDataSource alloc] initWithModelCellMap:modelCellMap cellConfigBlock:^(id cell, id item, NSIndexPath *indexPath) {
[cell configCellWithItem: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 支持任意方式创建,并可以混用。

Table View 的其他数据源方法和代理方法,通过 block 的方式扁平化处理。

dataSource.headerViewForSection = ^ UIView *(id sectionItem, NSInteger section){
//create headerView
return headerView;
};
dataSource.didSelectCellBlock = ^(id item, NSIndexPath *indexPath) {

};
dataSource.heightForRow = ^CGFloat (id item, NSIndexPath *indexPath) {
       	//height for different cell
return height;
};


dataSource 创建后,需要将绑定的table view 赋给它。

self.dataSource.tableView = self.tableView;


更多用法,可以参考项目Demo
https://github.com/jwfstars/WFDataSource

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: