分离tableview的datasource , 实现ViewController “瘦身”
2015-05-21 16:28
344 查看
分离tableview的datasource , 实现ViewController “瘦身”
这是一个实现轻量级ViewController的基本方法,大家通过这个代码页可以实现代理的分离.
#import <Foundation/Foundation.h>
typedef
void(^TableViewCellConfigureBlock) (id cell,id item);
@interface ZYYArrayDataSource :
NSObject<UITableViewDataSource>
-(id)initWithItems:(NSArray *)anItems cellItentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConofigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
#import "ZYYArrayDataSource.h"
#import "ZYYDataCell.h"
@interface
ZYYArrayDataSource()
@property (nonatomic,
strong) NSArray *items;
@property (nonatomic,
copy) NSString *cellIdentifier;
@property (nonatomic,
copy)
TableViewCellConfigureBlock configureCellBlock;
@end
@implementation ZYYArrayDataSource
- (id)init
{
return
nil;
}
-(id)initWithItems:(NSArray *)anItems cellItentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConofigureCellBlock
{
self = [super
init];
if (self) {
self.items = anItems;
self.cellIdentifier = aCellIdentifier;
self.configureCellBlock = [aConofigureCellBlock
copy];
}
return
self;
}
-(id)itemAtIndexPath:(NSIndexPath *)indexPath
{
return self.items[(NSUInteger)indexPath.row];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
ZYYDataCell * cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
if (cell== nil) {
cell = [[ZYYDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
}
id item = [self
itemAtIndexPath:indexPath];
self.configureCellBlock(cell,item);
return cell;
}
@end
//在viewController里面这么使用就可以了
UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.mTableView = tableView;
//为初始化列表的数据源声明一个block
TableViewCellConfigureBlock configureBlock = ^(ZYYDataCell *cell,NSString * titile){
[cell configureForData:titile];
};
//初始化列表的数据源
_dataSource = [[ZYYArrayDataSource alloc]initWithItems:self.mArrayData cellItentifier:@"ID" configureCellBlock:configureBlock];
self.mTableView.dataSource = _dataSource;
//把列表添加到View上
[self.view addSubview:self.mTableView];
这是一个实现轻量级ViewController的基本方法,大家通过这个代码页可以实现代理的分离.
#import <Foundation/Foundation.h>
typedef
void(^TableViewCellConfigureBlock) (id cell,id item);
@interface ZYYArrayDataSource :
NSObject<UITableViewDataSource>
-(id)initWithItems:(NSArray *)anItems cellItentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConofigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
#import "ZYYArrayDataSource.h"
#import "ZYYDataCell.h"
@interface
ZYYArrayDataSource()
@property (nonatomic,
strong) NSArray *items;
@property (nonatomic,
copy) NSString *cellIdentifier;
@property (nonatomic,
copy)
TableViewCellConfigureBlock configureCellBlock;
@end
@implementation ZYYArrayDataSource
- (id)init
{
return
nil;
}
-(id)initWithItems:(NSArray *)anItems cellItentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConofigureCellBlock
{
self = [super
init];
if (self) {
self.items = anItems;
self.cellIdentifier = aCellIdentifier;
self.configureCellBlock = [aConofigureCellBlock
copy];
}
return
self;
}
-(id)itemAtIndexPath:(NSIndexPath *)indexPath
{
return self.items[(NSUInteger)indexPath.row];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
ZYYDataCell * cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
if (cell== nil) {
cell = [[ZYYDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
}
id item = [self
itemAtIndexPath:indexPath];
self.configureCellBlock(cell,item);
return cell;
}
@end
//在viewController里面这么使用就可以了
UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.mTableView = tableView;
//为初始化列表的数据源声明一个block
TableViewCellConfigureBlock configureBlock = ^(ZYYDataCell *cell,NSString * titile){
[cell configureForData:titile];
};
//初始化列表的数据源
_dataSource = [[ZYYArrayDataSource alloc]initWithItems:self.mArrayData cellItentifier:@"ID" configureCellBlock:configureBlock];
self.mTableView.dataSource = _dataSource;
//把列表添加到View上
[self.view addSubview:self.mTableView];
相关文章推荐
- 打造轻量级ViewController之用Block分离TableView的dataSource
- iOS Controller 瘦身运动之 UItableView datasource 与 delegate的分离
- iOS实现UICollectionViewDataSource与Controller的分离
- ASP.NET MVC 中实现View与Controller分离
- ASP.NET MVC 中实现View与Controller分离
- ios常用之tableview的View和controller分离
- viewController 瘦身 -- 通过新建DataSource类来管理tableView的数据源方法
- 用UICollectionView及其controller相关UICollectionViewDataSource,UICollectionViewDelegate代理实现多列显示方法
- iOS UISearchController TableView 实现简单搜索功能
- iOS 瘦身ViewController 分离tableViewDataSource
- ASP.NET MVC 中实现View与Controller分离
- iOS DataSource从tableview分离 简化viewController
- tableview的代理分离,帮助控制瘦身
- C# Asp.Net MVC 3 项目实现控制器(controller)与页面(view)分项目管理
- 实现pushViewController:animated:的不同页面转换特效
- ios UISearchDisplayController 实现 UITableView 搜索功能
- iOS使用pageViewController实现多视图滑动切换
- IOS开发笔记——实现tableView的headerView跟随cell滑动
- iOS开发使用sdWebImage实现对tableView的cell加载图片淡入动画
- 1.框架搭建-Model、View、Controller分离