打造轻量级ViewController之用Block分离TableView的dataSource
来源:互联网 发布:手机淘宝开店准备 编辑:程序博客网 时间:2024/05/07 21:20
各位大侠在开发的过程中可能会有这样的困惑:每次编写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
#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
}
#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;
}
- 打造轻量级ViewController之用Block分离TableView的dataSource
- 分离tableview的datasource , 实现ViewController “瘦身”
- iOS DataSource从tableview分离 简化viewController
- 打造轻量级 tableViewController 之抽离 DataSource/Delegate
- TableView dataSource
- ViewController之间的block传值
- android 之打造轻量级适配器
- 打造Android轻量级框架XSnow的后继之路
- 打造更轻量级的ViewControllers
- iOS开发之DataSource神奇魔法,优雅的写法让你轻松驾驭TableView
- 分离 UITableView 的 Delegate 和 Datasource
- 打造一个轻量级,简单,易用的Android Banner框架
- iOS Controller 瘦身运动之 UItableView datasource 与 delegate的分离
- 天气 -城市选择 的block 回调 ,用一个TableView实现
- (作业)TableView、Delegate、DataSource
- 打造轻量级的实体类数据容器
- iOS tableview的delegate和datasource执行顺序
- tableview的代理分离,帮助控制瘦身
- 把胸罩卖给男生
- python中的回调函数例子
- vimrc_frankxie
- 关于ARM9协处理器CP15及MCR和MRC指令
- Makefile讲义(4)——书写命令
- 打造轻量级ViewController之用Block分离TableView的dataSource
- Mac下的Eclipse的LogCat会自动截断超过1024字节的输出
- UITableView(三)--- UITableViewDataSource
- MongoDB学习(一):MongoDB 环境的搭建
- 编程算法 - 二分图判定 代码(C)
- C/C++中printf、cout输出顺序
- OCP 1Z0 053 213
- Android:Layout_weight的深刻理解
- 华为又有一位员工猝死