MUKDataSource
MUKDataSource
是一个设计用来提供数据的类(我想这并不令人惊讶)。它提供了一个结构来向视图提供不可变数据。当更改数据时,它会收到更新以应用。
MUKDataSource
既可以支持 UITableView
、UICollectionView
或 UIPageViewController
实例,但它的设计是为了容纳任何类型的数据,适用于任何类型的用法。
用法
要运行示例项目,请克隆仓库,然后首先从示例目录运行 pod install
要求
- iOS 7 SDK。
- 最低部署目标:iOS 7。
安装
MUKDataSource
通过 CocoaPods 可用。要安装它,只需将以下行添加到您的 Podfile 中
pod "MUKDataSource"
示例
假设您想在表视图中下载并显示花朵列表。
您可以随意下载数据,但 MUKContentFetch 是一个不错的选择。一旦您有了数据,您就设置分区,然后对表视图应用更新。应用更新意味着插入、删除、重新加载、移动分区和项目,并带有漂亮的动画。
// FlowersFetch contains download+parse logic
MUKContentFetch *fetch = [[FlowersFetch alloc] init];
[fetch startWithCompletionHandler:^(MUKContentFetchResponse *response) {
if (response.resultType == MUKContentFetchResultTypeSuccess) {
MUKDataSourceContentSection *section = [[MUKDataSourceContentSection alloc] initWithIdentifier:@"flowers" items:response.object header:@"Flowers" footer:nil];
MUKDataSourceTableUpdate *update = [self.dataSource setTableSections:@[section]];
[update applyToTableView:self.tableView withAnimation:[MUKDataSourceTableUpdateAnimation automaticAnimation]];
}
else if (response.resultType == MUKContentFetchResultTypeFailed) {
MUKDataSourceContentPlaceholder *placeholder = [[MUKDataSourceContentPlaceholder alloc] initWithTitle:@"Error" subtitle:[response.error localizedDescription] image:nil];
self.dataSource.content = placeholder;
}
}];
要在表视图中显示项目的数据源非常简单,因为您只需提供单元格。
@interface FlowersDataSource : MUKDataSource
@end
@implementation FlowersDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *const kCellIdentifier = @"FlowerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
}
Flower *flower = [self itemAtIndexPath:indexPath];
cell.textLabel.text = flower.name;
return cell;
}
@end
如果您继承自 MUKTableViewController
,则只需设置数据源,数据源将自动附加到表视图。
@interface FlowersTableViewController : MUKTableViewController
@end
@implementation FlowersTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.dataSource = [[FlowersDataSource alloc] init];
}
@end
作者
Marco Muccinelli,[email protected]
许可证
MUKDataSource
在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。