分离tableview的datasource , 实现ViewController “瘦身”

分离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];




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 iOS UITableView 的代码实现示例: 1. 首先,在你的视图控制器中添加 UITableView 属性: ``` @interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView; @end ``` 2. 在 viewDidLoad 方法中初始化 UITableView: ``` - (void)viewDidLoad { [super viewDidLoad]; // 初始化 UITableView self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; } ``` 3. 实现 UITableViewDataSource 协议中的方法: ``` // 返回 UITableView 中的 section 数量 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // 返回 UITableView 中某个 section 中的 row 数量 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } // 返回 UITableView 中某个 indexPath 的 cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MyCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = [NSString stringWithFormat:@"Cell %ld", (long)indexPath.row]; return cell; } ``` 4. 实现 UITableViewDelegate 协议中的方法,比如设置 cell 的高度: ``` - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44.0f; } ``` 以上就是一个简单的 UITableView 的代码实现示例。需要注意的是,UITableView 必须指定 delegate 和 dataSource,而且需要实现 UITableViewDataSource 和 UITableViewDelegate 协议中的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值