UITableview代理方法与Viewcontroller分离

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

DEMO

建议先下载demo,再结合下面的分析,会好理解点。地址https://github.com/Resory/RYDatasource

逻辑

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

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

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

实现

  • 在Viewcontroller.m中。我们命名了三个全局变量,如下代码

(configSelectedBlock不需要设置全局,到时候在函数中直接生成即可)

1
2
3
4
// Viewcontroller.m
@property (nonatomic, strong) NSMutableArray *serverData;        // 服务器返回的数据
@property (nonatomic, strong) NSMutableArray *cellIdentifiers;   // cell样式标示
@property (nonatomic, strong) TDatasource *datasource;           // 中间人
  • 还是在Viewcontroller.m中.我们把数据,复用,事件设置好后,看看如何调用中间人。

数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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];
}

复用

1
2
3
4
5
6
7
8
9
// Viewcontroller.m  
- (void)configIdentifier
{
 
     // cell复用设置
     _cellIdentifiers = [[NSMutableArray alloc] init];
     [_cellIdentifiers addObject:NSStringFromClass([TCellOne class])];
     [_tableview registerNib:[TCellOne nib] forCellReuseIdentifier:_cellIdentifiers[0]];
}

初始化"中间人"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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的协议方法执行者

1
2
3
4
5
6
7
8
9
// Viewcontroller.m  
- (void)configTableView
{
 
     // 把_dataSource设置成_tableview的代理,以后所有代理方法都在_dataSource实现
     _tableview.delegate = _datasource;
     _tableview.dataSource = _datasource;
     _tableview.tableFooterView = [UIView  new ];
}
  • 我们进去中间人看看它到底做了什么,下面的代码可以清晰看到。中间人除了多了初始化方法来接受数据外,其他方法都是UITableview的协议方法

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
30
31
32
33
// 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里面的方法,也是一眼能看懂的代码··

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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];
     }
}
  • 挫就挫吧,总得来张图。

1159720-7b46653d16d54f5d.png

最后

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值