打造轻量级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

接下来我们看下我们自定义的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;

}







0 0
原创粉丝点击