在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的代碼更加簡潔,代碼的重用性變高。
思路來源于objcio
以上代碼在GitHub可以下載,僅供參考