iOS-UITableview代理方法与Viewcontroller分离

作者:Resory地址:

在objcio.cn中有一篇文章更轻量的 View Controllers : 其中有一小节,是说把UITableview的datasource和delegate分离出Viewcontroller的。我也试着实现了一下,随便把思路总结下~

DEMO

建议先下载demo,再结合下面的分析,会好理解点。地址

逻辑

既然我们要把UITableview的协议方法分离出来,就得有一个人去接,在这里我们把这个人叫Datasource。可以理解它为中间人。这个中间人就是以后实现UITableview协议方法的地方。

要中间人实现UITableview协议方法,就得知道UITableview的数据,复用,事件等要素。这些数据由Viewcontroller来传。这三个要素分别定义为serverData,cellIdentifiers,configSelectedBlock

自此我们可以知道,只要Viewcontroller传了serverData,cellIdentifiers,configSelectedBlock这三个基本参数,中间人就可以实现UITableview协议方法了。

实现

在Viewcontroller.m中。我们命名了三个全局变量,如下代码(configSelectedBlock不需要设置全局,到时候在函数中直接生成即可)

// Viewcontroller.m @property (nonatomic, strong) NSMutableArray *serverData; // 服务器返回的数据 @property (nonatomic, strong) NSMutableArray *cellIdentifiers;// cell样式标示 @property (nonatomic, strong) TDatasource *datasource;// 中间人

还是在Viewcontroller.m中.我们把数据,复用,事件设置好后,看看如何调用中间人。

数据

// Viewcontroller.m - (void)configData { // 服务器返回的数据 _serverData = [[NSMutableArray alloc] init]; // 实体设置 TModelOne *one = [[TModelOne alloc] init]; TModelOne *two = [[TModelOne alloc] init]; one.name = @"奇犽"; one.imageName = @"001.jpg"; two.name = @"拿尼加"; two.imageName = @"002.jgp"; [_serverData addObject:one]; [_serverData addObject:two]; }

复用

// Viewcontroller.m - (void)configIdentifier { // cell复用设置 _cellIdentifiers = [[NSMutableArray alloc] init]; [_cellIdentifiers addObject:NSStringFromClass([TCellOne class])]; [_tableview registerNib:[TCellOne nib] forCellReuseIdentifier:_cellIdentifiers[0]]; }

初始化”中间人”

// Viewcontroller.m - (void)configDataSource { // cell数据 [self configData]; // cell复用 [self configIdentifier]; // cell事件 RYCellSelectedBlock cellSelectedBlock = ^(id obj){ // cell点击事件 [self cellSelectedWithObj:obj]; }; // 初始化dataSource _datasource = [[TDatasource alloc] initWithServerData:_serverData andCellIdentifiers:_cellIdentifiers]; _datasource.cellSelectedBlock = cellSelectedBlock; }

把中间人设置为UITableview的协议方法执行者

// Viewcontroller.m - (void)configTableView { // 把_dataSource设置成_tableview的代理,以后所有代理方法都在_dataSource实现 _tableview.delegate = _datasource; _tableview.dataSource = _datasource; _tableview.tableFooterView = [UIView new]; }

我们进去中间人看看它到底做了什么,下面的代码可以清晰看到。中间人除了多了初始化方法来接受数据外,其他方法都是UITableview的协议方法

// TDatasource.m - (id)initWithServerData:(NSArray *)serverData andCellIdentifiers:(NSArray *)identifiers { self = [super init]; if(self) { self.serverData = serverData; // 数据 self.cellIdentifiers = identifiers; // 复用 } return self; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifiers[0] forIndexPath:indexPath]; // cell的数据填充(cell的category方法) [cell configCellWithEntity:self.serverData[indexPath.row]]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80.0; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // cell点击事件 self.cellSelectedBlock(indexPath); }

最后我们来看下cell里面的方法,也是一眼能看懂的代码··

#import "TCellOne.h" #import "TModelOne.h" @implementation TCellOne - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)configCellWithEntity:(id)entity { if(entity) { TModelOne *model = entity; self.name.text = model.name; self.avartar.image = [UIImage imageNamed:model.imageName]; } }

挫就挫吧,总得来张图。。

最后

UITableview协议方法分离出Viewcontroller后,Viewcontroller确实清爽了不少。但同时也带了不便,比如说又多了一个文件,又比如说cell的点击事件就得用一个block来回调,如果是cell里面的子view的点击事件就更不利索了。所以有利就有弊吧。还是得结合实际选择最适合自己的开发方式~

如果你有疑问或者发现错误请留言给我

喜欢就点个👍,点个星什么的。3Q~~