每个人都在编写自己的基于 block 的 TableView/CollectionView/PageViewController DataSource,所以这是我的。
只需添加
pod 'CJADataSource'
到您的 Podfile
并使用 #import <CJADataSource/CJADataSource>
导入所需的文件
或者直接将 CJADataSource 文件夹中的文件包含到您的项目中。
UITableView *tableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, 1, 1)];
...
...
[self.view addSubview: self.tableView];
NSDictionary *identifiers = @{ @"UITableViewCell" : [UITableViewCell class] };
NSArray *items = @[@"Item 1", @"Item 2", @"Item 3", @"Item 4"];
CJAArrayDatasource *datasource = [[CJAArrayDatasource alloc] initWithItems:items
tableViewIdentifiersAndCellClasses:identifiers];
datasource.configureCellBlock = ^(UITableView *tableView, NSIndexPath *indexPath, UITableViewCell *cell, NSString *text){
cell.textLabel.text = text;
};
datasource.cellClickedBlock = ^(UITableView *tableView, NSIndexPath *indexPath, NSString *text){
NSLog(@"%@", text);
};
datasource.tableView = self.tableView;
见 许可证