DBDataSource是一种易于使用的数据源,符合UITableView和UICollectionView,可以轻松添加到项目中,以便快速填充tableview和collectionview。使用CellConfigureBlock来填充cell内容,并用数组初始化数据源。
包含了一个示例项目。克隆仓库并打开“DBDataSourceDemo.xcworkspace”。
使用字符串数组和一个默认的tablecell的基本示例
CellConfigureBlock configureCell = ^(UITableViewCell *cell, NSString *item) {
cell.textLabel.text = item;
};
NSArray *myArray = @[@"Demo data 1", @"Demo data 2", @"Demo data 3"];
self.demoDataSource = [[DBDataSource alloc] initWithArray:myArray cellIdentifier:DemoCellIdentifier configureCellBlock:configureCell];
self.tableView.dataSource = self.demoDataSource;
另一个示例,使用Person对象的数组和一个自定义的collectionviewcell
CellConfigureBlock configureCell = ^(DemoCollectionCell *cell, Person *person) {
[cell populateWithPerson:person];
};
NSArray *myArray = @[
[Person personWithFirstName:@"Daniel" lastName:@"Bowden" emailAddress:@"[email protected]"],
[Person personWithFirstName:@"Buster" lastName:@"Posey" emailAddress:@"[email protected]"],
[Person personWithFirstName:@"Pablo" lastName:@"Sandoval" emailAddress:@"[email protected]"]
];
self.demoDataSource = [[DBDataSource alloc] initWithArray:myArray cellIdentifier:[DemoCollectionCell cellIdentifier] configureCellBlock:configureCell];
self.collectionView.dataSource = self.demoDataSource;
要访问数据源以便在如didSelectSelectItemAtIndexPath
或heightForRowAtIndexPath
这样的方法中使用,您可以使用[self.demoDataSource itemAtIndexPath:indexPath];
Daniel Bowden, danielbowden。
DBDataSource遵守MIT许可证。有关更多信息,请参阅LICENSE文件。