版权所有,禁止匿名转载;禁止商业使用。
在IOS开发中采用了MVC得模式,ViewController通常是最庞大的文件,里面包含了各种各样的大概,造成代码的复用率低下,可读性也降低,那么有什么办法来解决这个问题呢。
在创建Table的时候,绑定数据源需要实现三个委托
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
每次写表格都要重新实现这三个方法,有没有什么方法可以复用这些代码呢。
我们来分析一下,一般表格的数据源都是一个数组。Cell的数量就是数组的数量,那么既然找到了规律,我们就可以吧这一部分的代码提取出来。
分离DataScource
我们先来新建一个类叫做 ArrayDataSource
首先有一个
- (id)initWithItems:(NSArray *)anItems multipleItem:(BOOL)multipleItem cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
方法用来初始化。它有四个参数,第一个 items ,很显然这个是数据源的数组,第二个 multipleItem 是一个BOOL类型的值,用来说明传入的数组是否是多维数组,用以Group类型的表格数据源传入,第三个 aCellIdentifier 是cell的标识符,复用cell的时候会用到,第四个参数是 aConfigureCellBlock 一个配置cell的Block回调方法。
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef void (^TableViewCellConfigureBlock)(id cell, id item); @interface ArrayDataSource : NSObject <UITableViewDataSource,UITableViewDelegate> /** * 初始化表格方法 * 根据数据源自动计算出数量,支持二位数组 * 使用二维数组的时候,将multipleItem值设为YES即可完成数据的输入 * * @param anItems 数据源 * @param aCellIdentifier cell标示符 * @param aConfigureCellBlock 配置cell方法的block回调方法 * * @return id */ - (id)initWithItems:(NSArray *)anItems multipleItem:(BOOL)multipleItem cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock; - (id)itemAtIndexPath:(NSIndexPath *)indexPath; @end ArrayDataSource.h @implementation ArrayDataSource - (id)initWithItems:(NSArray *)anItems multipleItem:(BOOL)multipleItem cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock { self = [super init]; if (self) { _items = anItems; _cellIdentifier = aCellIdentifier; _configureCellBlock = [aConfigureCellBlock copy]; _isMultiple=multipleItem; } return self; } - (id)itemAtIndexPath:(NSIndexPath *)indexPath { return [_items objectAtIndex:indexPath.row]; } - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { if(_isMultiple) { return [[_items objectAtIndex:section] count]; } else { return [_items count]; } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (_isMultiple) { return [_items count]; } else { return 1; } } - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { id cell = [tableView dequeueReusableCellWithIdentifier:_cellIdentifier forIndexPath:indexPath]; if (_isMultiple) { NSArray *Sectionitem = [_items objectAtIndex:indexPath.section]; self.configureCellBlock(cell, [Sectionitem objectAtIndex:indexPath.row]); //self.configureCellBlock(cell, @"fdgdf"); } else { id item = [self itemAtIndexPath:indexPath]; self.configureCellBlock(cell, item); } return cell; }
以上就是这个简单类的部分代码,接下来我们来看一下如何简单的实现数据源的传入
TableViewCellConfigureBlock configureCell = ^(UITableViewCell *cell, NSString *photo) { cell.textLabel.text=photo; }; NSArray *item1= @[@"sasewrwer",@"f22f",@"fff"]; NSArray *item2= @[@"sas",@"ff",@"gfdfsd"]; NSArray *item= @[item1,item2]; _photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:item multipleItem:YES cellIdentifier:@"cell" configureCellBlock:configureCell]; self.tableView.dataSource = _photosArrayDataSource; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
只需要三四行代码即可完成Table的数据源传入,而不用再去实现Delegate,这样使得ViewController的代码更加简洁,代码的重用性变高。
以上代码在 GitHub 可以下载,仅供参考