之前写tableview比较多,所以写了很多重复的DataSource相关的代码,控制器也比较大,所以一直想尝试一种新的写法。好在今天看到了相关的例子。替自己记录下。参考 https://www.cnblogs.com/zhouxihi/p/6204195.html
eg: 1.创建一个继承NSOject的类
2.继承和实现dataSource 协议
#import //不导入这个,报错
typedef void(^ConfigTableViewBlock)(id cell ,id item);
@interface TabViewDatas : NSObject
@property (nonatomic,strong) NSArray * datas;
@property (nonatomic,copy) NSString * cellIdentifier;
@property (nonatomic,copy) ConfigTableViewBlock aConfigTableViewBlock;
-(instancetype)initWithDatas:(NSArray *)datas
cellIdentifiers:(NSString *)identifiers
ConfigTableViewBlock:(ConfigTableViewBlock)aConfigTableViewBlock;
-(id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
@implementation TabViewDatas
-(instancetype)init{
return nil; //必须为nil
}
-(instancetype)initWithDatas:(NSArray *)datas cellIdentifiers:(NSString *)identifiers ConfigTableViewBlock:(ConfigTableViewBlock)aConfigTableViewBlock{
if (self = [super init]) {
_datas = datas;
_cellIdentifier = identifiers;
_aConfigTableViewBlock = [aConfigTableViewBlock copy];
}
return self;
}
-(id)itemAtIndexPath:(NSIndexPath *)indexPath{
return self.datas[indexPath.row];
}
#pragma mark -tabViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.datas.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
}
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.text = self.datas[indexPath.row];
id item = [self itemAtIndexPath:indexPath];
self.aConfigTableViewBlock(cell, item);
return cell;
}
@end
//test
@interface ViewController : UIViewController
@end
#import "TabViewDatas.h"
#import "TestViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * datas;
@property (nonatomic,strong) TabViewDatas * tabViewDataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.datas = [NSMutableArray array];
for (NSInteger i = 0; i< 200; i++) {
NSString *str = [NSString stringWithFormat:@"%ld",i];
[self.datas addObject:str];
}
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView = tableView;
ConfigTableViewBlock block = ^(UITableViewCell *cell,NSString *str){
NSLog(@"cell:%@ ,str:%@",cell,str);
};
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
self.tabViewDataSource = [[TabViewDatas alloc] initWithDatas:self.datas cellIdentifiers:@"cell" ConfigTableViewBlock:block];
self.tableView.dataSource = self.tabViewDataSource;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TestViewController *testVC = [[TestViewController alloc] init];
[self presentViewController:testVC animated:YES completion:nil];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
@interface TestViewController : UIViewController
@end
#import "TabViewDatas.h"
@interface TestViewController ()
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSArray * datas;
@property (nonatomic,strong) TabViewDatas * tabViewDataSource;
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
self.datas = @[@"6",@"7",@"8",@"9",@"10"];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView = tableView;
ConfigTableViewBlock block = ^(UITableViewCell *cell,NSString *str){
NSLog(@"cell:%@ ,str:%@",cell,str);
//在这里操作cell中的控件,或者给cell中的控件赋值
};
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
self.tabViewDataSource = [[TabViewDatas alloc] initWithDatas:self.datas cellIdentifiers:@"cell" ConfigTableViewBlock:block];
self.tableView.dataSource = self.tabViewDataSource;
[self.view addSubview:self.tableView];
}
记录这个,主要是因为他给我提供了一个思路。以后可以在别的地方做出相类似的尝试,再次感谢原创作者。