轻量级ViewController,分离UITableView的代理

简介

最近读到了一篇文章(原文),里面讲到ViewControler通常比较臃肿,可以通过分离UITableView的代理的方式,给ViewControler瘦身,结合文章和自己的实际情况,写了一个小demo,demoGitHub地址

1.创建一个继承NSObject的类,然后将可变的地方写成属性和参数
#import 
#import 
// 返回cell和数据,这里数据类型看数组里装的是什么类型,这里是NSString
typedef void(^returnCell)(id cell, NSString *data);
// 返回点击cell的数据,类型同上,不需要数据就不写返回值
typedef void(^returnClick)(NSString *data);

@interface CJDataSource : NSObject 
// 返回cell
@property (nonatomic, copy)returnCell rBlock;
// 返回click
@property (nonatomic, copy)returnClick rClickBlock;
// cell重用标识
@property (nonatomic, strong) NSString *identifier;
// 数据源
@property (nonatomic, strong) NSArray *dataArr;
// cell的类名
@property (nonatomic, strong) NSString *cellCalass;
// 初始化方法
- (instancetype)initWithDataArr:(NSArray *)arr
                 withIdentifier:(NSString *)identifier
                      cellClass:(NSString *)cellClass;

@end

2.实现初始化方法,以及tableView的代理方法

#pragma mark 初始化
- (instancetype)initWithDataArr:(NSArray *)arr
                 withIdentifier:(NSString *)identifier
                      cellClass:(NSString *)cellClass
{
    self.dataArr = [NSArray arrayWithArray:arr];
    self.identifier = identifier;
    self.cellCalass = cellClass;
    return self;
}

#pragma mark tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.identifier];
    if (!cell) {
        // 用传进来的类 创建cell
        cell = [[NSClassFromString(self.cellCalass) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.identifier];
    }
    // cell回调
    if (self.rBlock) {
        self.rBlock(cell, self.dataArr[indexPath.row]);
    }
    return cell;
}

#pragma mark tableview delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@", self.dataArr[indexPath.row]);
    // 点击回调
    if (self.rClickBlock) {
        self.rClickBlock(self.dataArr[indexPath.row]);
    }
}
3.在ViewController中创建tableView以及DataSource
#import "ViewController.h"
#import "CJDataSource.h"
#import "CJTableViewCell.h"
#import "CJViewController.h"

static NSString *const reuse = @"reuse";

@interface ViewController ()

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) CJDataSource *dataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    // cell回调
    self.dataSource.rBlock = ^(id cell, id data){
        // cell赋值方法
        [cell setCellData:data];
    };
    // 点击事件回调
    __weak typeof(self) wakSelf = self;
    self.dataSource.rClickBlock = ^(NSString *data){
        // 跳转到另一个控制器,用同样的方式创建一个tableview,换一个自定义的cell--CJTableViewCell2,验证是否能重用DataSource
        CJViewController *VC = [CJViewController new];
        [wakSelf presentViewController:VC animated:YES completion:nil];
    };
}

#pragma mark 加载dataSource
- (CJDataSource *)dataSource
{
    if (!_dataSource) {
        // 这里的数组应该是请求下来的数组,这里就简写了
        _dataSource = [[CJDataSource alloc] initWithDataArr:@[@"1", @"2", @"3"] withIdentifier:reuse cellClass:@"CJTableViewCell"];
    }
    return _dataSource;
}

#pragma mark 加载tableview
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.dataSource = self.dataSource;
        _tableView.delegate = self.dataSource;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
4.验证重用,新建一个ViewController,用同样的方法创建TableView,这里就不贴代码了,可以去GitHub下载demoGitHub地址,写法和上面的相同。

将代理分离之后,ViewController的代码量明显减少,具体的封装内容,可以根据自己项目需求来修改。

结语:限于水平,本文只写了一些基本用法和注意事项,如果文中存在错误请指出,我会及时修改。

你可能感兴趣的:(轻量级ViewController,分离UITableView的代理)