iOS DataSource从tableview分离 简化viewController
2016-04-18 17:14
459 查看
通过分离dataSource 让我们的code具有更高的复用性.
转载自汪海的实验室
一 定义dataSource
dataSource.h
typedef void (^TableViewCellConfigureBlock)(id cell, id item);
@interface GroupNotificationDataSource : NSObject<UITableViewDataSource>
- (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
dataSource.m
@implementation GroupNotificationDataSource
{
NSArray *_itemsArray;
NSString *_cellIdentifier;
TableViewCellConfigureBlock _configureCellBlock;
}
- (id)init{
return nil;
}
- (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock{
self = [super init];
if (self) {
_itemsArray = anItems;
_cellIdentifier = aCellIdentifier;
_configureCellBlock = [aConfigureCellBlock copy];
}
return self;
}
- (id)itemAtIndexPath:(NSIndexPath *)indexPath {
return _itemsArray[(NSUInteger) indexPath.row];
}
#pragma mark tableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _itemsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// NOTE: This method can return nil so you need to account for that in code
GroupNotificationCell *cell=[tableView dequeueReusableCellWithIdentifier:_cellIdentifier];
// NOTE: Add some code like this to create a new cell if there are none to reuse
if (cell==nil) {
cell=[[GroupNotificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_cellIdentifier];
}
id item = [self itemAtIndexPath:indexPath];
_configureCellBlock(cell, item);
return cell;
}
其中,需要注意tableView:cellForRowAtIndexPath:方法中对于cell的重用
二 重写cell的初始化
cell.h
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
cell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self configureUI];
}
return self;
}
三 controller中设置tableview的dataSource
_dataSource=[[GroupNotificationDataSource alloc]initWithItems:[NSArray arrayWithObjects:@"1",@"2", nil] cellIdentifier:CellIdentifier configureCellBlock:^(id cell, id item) {
//
}];
_tableview.dataSource=_dataSource;
需要注意的是_dataSource应该是成员变量,而非局部变量,否则会报错:message send to delloced object
转载自汪海的实验室
一 定义dataSource
dataSource.h
typedef void (^TableViewCellConfigureBlock)(id cell, id item);
@interface GroupNotificationDataSource : NSObject<UITableViewDataSource>
- (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
dataSource.m
@implementation GroupNotificationDataSource
{
NSArray *_itemsArray;
NSString *_cellIdentifier;
TableViewCellConfigureBlock _configureCellBlock;
}
- (id)init{
return nil;
}
- (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock{
self = [super init];
if (self) {
_itemsArray = anItems;
_cellIdentifier = aCellIdentifier;
_configureCellBlock = [aConfigureCellBlock copy];
}
return self;
}
- (id)itemAtIndexPath:(NSIndexPath *)indexPath {
return _itemsArray[(NSUInteger) indexPath.row];
}
#pragma mark tableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _itemsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// NOTE: This method can return nil so you need to account for that in code
GroupNotificationCell *cell=[tableView dequeueReusableCellWithIdentifier:_cellIdentifier];
// NOTE: Add some code like this to create a new cell if there are none to reuse
if (cell==nil) {
cell=[[GroupNotificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_cellIdentifier];
}
id item = [self itemAtIndexPath:indexPath];
_configureCellBlock(cell, item);
return cell;
}
其中,需要注意tableView:cellForRowAtIndexPath:方法中对于cell的重用
二 重写cell的初始化
cell.h
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
cell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self configureUI];
}
return self;
}
三 controller中设置tableview的dataSource
_dataSource=[[GroupNotificationDataSource alloc]initWithItems:[NSArray arrayWithObjects:@"1",@"2", nil] cellIdentifier:CellIdentifier configureCellBlock:^(id cell, id item) {
//
}];
_tableview.dataSource=_dataSource;
需要注意的是_dataSource应该是成员变量,而非局部变量,否则会报错:message send to delloced object
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- div+css在思路和流程上实现结构与表现的分离分析
- 浅析PHP原理之变量分离/引用(Variables Separation)
- sql server2005实现数据库读写分离介绍
- 关于Asp代码与页面的分离模板技术第1/3页
- 用sql语句实现分离和附加数据库的方法
- 实用的js 焦点图切换效果 结构行为相分离
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题