美文网首页
更轻量的View Controllers

更轻量的View Controllers

作者: 一路上有你Hi | 来源:发表于2017-03-06 10:33 被阅读0次

View controllers 通常是 iOS 项目中最大的文件,并且它们包含了许多不必要的代码。所以 View controllers 中的代码几乎总是复用率最低的。我们将会看到给 view controllers 瘦身的技术,让代码变得可以复用,以及把代码移动到更合适的地方。

Data Source 分离出来

关于View Controllers的瘦身技术,大家也知道一些技巧,例如把model、view层单独分离出来,网络请求不在controller中完成。这篇文章介绍把 UITableViewDataSource 的代码提取出来放到一个单独的类中,是为 view controller 瘦身的强大技术之一。提取出来的类可以在项目中复用。

通常在项目中,会看到Data Source的以下三个代理方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (BankModel*)modelAtIndexPath:(NSIndexPath*)indexPath {
    return modelArray[(NSUInteger)indexPath.row];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectedBankCardCell * cell = [tableView dequeueReusableCellWithIdentifier:selectedBankCardCellID forIndexPath:indexPath];
    SelectedBankCardModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    if ([model.userBankId isEqualToString:_userBankId]) {
        cell.selectedButton.selected = YES;
        _selectedIndex = indexPath.row;
    }
    return cell;
}

这些代码基本都是围绕数组做一些事情,更针对地说,是围绕 view controller 所管理的 modelArray 数组做一些事情。我们可以尝试把数组相关的代码移到单独的类中。我们使用一个 block 来设置 cell,也可以用 delegate 来做这件事,这取决于你的习惯。

代码示例

新建一个类,.h文件如下:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef void(^TableViewCellConfigureBlock)(id cell ,id item);

@interface YFYArrayDataSource : NSObject <UITableViewDataSource>

- (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier cellConfigureBlock:(TableViewCellConfigureBlock)aCellConfigureBlock;

- (id)itemAtIndexPath:(NSIndexPath *)indexPath;

@end

.m文件的实现

#import "YFYArrayDataSource.h"

@interface YFYArrayDataSource ()

@property (nonatomic,strong)NSArray *items;
@property (nonatomic,copy)NSString *cellIndentifier;
@property (nonatomic,copy)TableViewCellConfigureBlock cellConfigureBlock;

@end

@implementation YFYArrayDataSource

- (id)init{
    return nil;
}

- (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier cellConfigureBlock:(TableViewCellConfigureBlock)aCellConfigureBlock{
    
    self = [super init];
    if (self) {
        self.items = anItems;
        self.cellIndentifier = aCellIdentifier;
        self.cellConfigureBlock = [aCellConfigureBlock copy];
    }
    return self;
}

- (id)itemAtIndexPath:(NSIndexPath *)indexPath{
    return self.items[(NSInteger) indexPath.row];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIndentifier forIndexPath:indexPath];
    id item = [self itemAtIndexPath:indexPath];
    self.cellConfigureBlock(cell,item);
    return cell;
}

现在,你可以把 view controller 中的这 3 个方法去掉了,取而代之,你可以创建一个 ArrayDataSource 类的实例作为 table view 的 data source。

- (void)creatTabelView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DBScreenWidth, DBScreenHeight - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    TableViewCellConfigureBlock configureCell = ^(MessageTableViewCell *cell,MessageModel *model){
        cell.model = model;
    };
    self.dataSource = [[YFYArrayDataSource alloc] initWithItems:_modelArray cellIdentifier:@"MessageTableViewCell" cellConfigureBlock:configureCell];
    self.tableView.dataSource = self.dataSource;
    
    [_tableView registerNib:[UINib nibWithNibName:@"MessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"MessageTableViewCell"];
     
}

现在你不用担心把一个 index path 映射到数组中的位置了,每次你想把这个数组显示到一个 table view 中时,你都可以复用这些代码。你也可以实现一些额外的方法,比如 tableView:commitEditingStyle:forRowAtIndexPath:,在 table view controllers 之间共享。

相关文章

网友评论

      本文标题:更轻量的View Controllers

      本文链接:https://www.haomeiwen.com/subject/tjbygttx.html