打造轻量级ViewController之用Block分离TableView的dataSource
2014-07-27 11:08
375 查看
各位大侠在开发的过程中可能会有这样的困惑:每次编写tableview的代码都要写那几个方法,而且必备的方法一个不能少,而这些代码往往会使得我们的ViewController类看起来很复杂,那么有没有什么办法把tableview的这些必备方法分离开来呢.答案是肯定的,下面我们就来探讨下用Block来分离tableview的dataSource
设计的思路是将tableview的dataSource 放在一个单独的类中,这个类实现tableView的dataSource协议,然后在ViewController里面使用的时候实例化这个类,然后赋给tableview的dataSource.如果你多次尝试这样的设计,也许你会发现如何设计一个可以复用的tableview的dataSource类.下面我们看码:
首先声明一个DataSource类如下:
#import <Foundation/Foundation.h>
typedef void(^TableViewCellConfigureBlock) (id cell,id item);
@interface LYHArrayDataSource : NSObject<UITableViewDataSource>
-(id)initWithItems:(NSArray *)anItems cellItentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConofigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
实现部分:
#import "LYHArrayDataSource.h"
#import "LYHDataCell.h"
@interface LYHArrayDataSource()
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
@end
@implementation LYHArrayDataSource
- (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
{
LYHDataCell * cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
if (cell== nil) {
cell = [[LYHDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
}
id item = [self itemAtIndexPath:indexPath];
self.configureCellBlock(cell,item);
return cell;
}
@end
接下来我们看下我们自定义的tableViewCell:
#import <UIKit/UIKit.h>
@interface LYHDataCell : UITableViewCell
- (void)configureForData:(NSString *)data;
@end
实现部分:
#import "LYHDataCell.h"
@interface LYHDataCell()
@property (retain,nonatomic) UILabel * mTitle;
@end
@implementation LYHDataCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.mTitle = [[UILabel alloc]initWithFrame:CGRectMake(30, 10, 200, 40)];
[self.contentView addSubview:self.mTitle];
}
return self;
}
- (void)configureForData:(NSString *)data
{
self.mTitle.text = data;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
现在呢,我们就可以看看如何在ViewController里面如何使用这个类:
#import <UIKit/UIKit.h>
@interface LYHTableViewController : UIViewController<UITableViewDelegate>
@property (strong,nonatomic) UITableView * mTableView;
@property (strong,nonatomic) NSArray * mArrayData;
@end
实现部分:
#import "LYHTableViewController.h"
#import "LYHArrayDataSource.h"
#import "LYHDataCell.h"
@interface LYHTableViewController ()
{
LYHArrayDataSource * _dataSource;
}
@end
@implementation LYHTableViewController
@synthesize mArrayData;
@synthesize mTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray * array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
self.mArrayData = array;
[self setUpTableView];
}
- (void)setUpTableView
{
UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.mTableView = tableView;
TableViewCellConfigureBlock configureBlock = ^(LYHDataCell *cell,NSString * titile){
[cell configureForData:titile];
};
_dataSource = [[LYHArrayDataSource alloc]initWithItems:self.mArrayData cellItentifier:@"ID" configureCellBlock:configureBlock];
self.mTableView.dataSource = _dataSource;
[self.view addSubview:self.mTableView];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
设计的思路是将tableview的dataSource 放在一个单独的类中,这个类实现tableView的dataSource协议,然后在ViewController里面使用的时候实例化这个类,然后赋给tableview的dataSource.如果你多次尝试这样的设计,也许你会发现如何设计一个可以复用的tableview的dataSource类.下面我们看码:
首先声明一个DataSource类如下:
#import <Foundation/Foundation.h>
typedef void(^TableViewCellConfigureBlock) (id cell,id item);
@interface LYHArrayDataSource : NSObject<UITableViewDataSource>
-(id)initWithItems:(NSArray *)anItems cellItentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConofigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
实现部分:
#import "LYHArrayDataSource.h"
#import "LYHDataCell.h"
@interface LYHArrayDataSource()
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
@end
@implementation LYHArrayDataSource
- (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
{
LYHDataCell * cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
if (cell== nil) {
cell = [[LYHDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
}
id item = [self itemAtIndexPath:indexPath];
self.configureCellBlock(cell,item);
return cell;
}
@end
接下来我们看下我们自定义的tableViewCell:
#import <UIKit/UIKit.h>
@interface LYHDataCell : UITableViewCell
- (void)configureForData:(NSString *)data;
@end
实现部分:
#import "LYHDataCell.h"
@interface LYHDataCell()
@property (retain,nonatomic) UILabel * mTitle;
@end
@implementation LYHDataCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.mTitle = [[UILabel alloc]initWithFrame:CGRectMake(30, 10, 200, 40)];
[self.contentView addSubview:self.mTitle];
}
return self;
}
- (void)configureForData:(NSString *)data
{
self.mTitle.text = data;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
现在呢,我们就可以看看如何在ViewController里面如何使用这个类:
#import <UIKit/UIKit.h>
@interface LYHTableViewController : UIViewController<UITableViewDelegate>
@property (strong,nonatomic) UITableView * mTableView;
@property (strong,nonatomic) NSArray * mArrayData;
@end
实现部分:
#import "LYHTableViewController.h"
#import "LYHArrayDataSource.h"
#import "LYHDataCell.h"
@interface LYHTableViewController ()
{
LYHArrayDataSource * _dataSource;
}
@end
@implementation LYHTableViewController
@synthesize mArrayData;
@synthesize mTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray * array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
self.mArrayData = array;
[self setUpTableView];
}
- (void)setUpTableView
{
UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.mTableView = tableView;
TableViewCellConfigureBlock configureBlock = ^(LYHDataCell *cell,NSString * titile){
[cell configureForData:titile];
};
_dataSource = [[LYHArrayDataSource alloc]initWithItems:self.mArrayData cellItentifier:@"ID" configureCellBlock:configureBlock];
self.mTableView.dataSource = _dataSource;
[self.view addSubview:self.mTableView];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
相关文章推荐
- 分离tableview的datasource , 实现ViewController “瘦身”
- iOS Controller 瘦身运动之 UItableView datasource 与 delegate的分离
- iOS实现UICollectionViewDataSource与Controller的分离
- iOS DataSource从tableview分离 简化viewController
- ios常用之tableview的View和controller分离
- 导入runtime扩展NSObject,利用block在ViewController中来进行消息传递
- 导入runtime扩展NSObject,利用block在ViewController中来进行消息传递(二)
- UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath
- [ios开发基础之 Core Data[3]]如何使用NSFetchedResultsController 来集成TableView操作
- Navigation 与 tableview controller 一起使用
- viewController 和 tableView 相关方法执行顺序及作用
- UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
- 一款着眼性能的轻量级Data Grid——WPF Table View
- 为什么要编写轻量级的View Controller??
- 如何拆分View Controller进而实现轻量级的View Controller[UIKit]
- 构建轻量级的Table View注意事项[UIKit]
- ASP.NET MVC 中实现View与Controller分离
- 普通viewController绘制tableview tableviewcell
- 在presentModalViewController出来的TableView中使用pushViewController
- Use Storyboards to Build Navigation Controller and Table View