创建静态表格视图数据源的一种更简单的方式。
为了使代码更清晰并减少硬编码,WAMCellInfo 和 WAMSectionInfo 都可以为索引搜索、追加、替换和删除设置别名(根据您的决定是否可为 null)。
WAMCellInfo
是一个类,用于存储您的单元格的基本信息。您可以创建一个具有 reuseIdentifier 的单元格信息如下
WAMCellInfo *cellInfo = [WAMCellInfo infoWithReuseIdentifier:@"identifier" title:nil detail:nil alias:@"infoWithReuseIdentifier"];
或使用您定制的单元格
WAMCellInfo *cellInfo = [WAMCellInfo infoWithSelfDefineCell:self.customizedCell alias:@"infoWithSelfDefineCell"];
WAMSectionInfo 存储了同一部分中的所有 cellInfos。您只需简单地将 cellInfos 添加到 sectionInfo
WAMSectionInfo *sectionInfo = [WAMSectionInfo infoWithCellInfos:@[cellInfoZero, cellInfoOne] alias:@"sectionAlias"];
对于具有特定别名的 cellInfo 的索引搜索非常简单(如果在 sectionInfo 中找不到索引,则函数将返回 NSNotFound)
NSUInteger index = [sectionInfo indexOfCellInfoWithAlias:@"forkYou"];
[sectionInfo appendingCellInfo:cellInfo];
if ([sectionInfo appendingCellInfo:cellInfo atIndex:2333]) {
// do something
} else {
// do something
}
[sectionInfo removeCellInfo:cellInfo];
[sectionInfo removeCellInfoAtIndex:0];
[sectionInfo removeCellInfoWithAlias:@"forkYou"];
[sectionInfo replaceCellInfo:originalCellInfo with:cellInfo];
[sectionInfo replaceCellInfoAtIndex:0 with:cellInfo];
[sectionInfo replaceCellInfoWithAlias:@"forkYou" with:cellInfo];
以下是一个简单的示例,说明如何在代码中使用 WAMDataSource
(有关更详细的信息,请查看 WAMCellInfo
、WAMSectionInfo
和 WAMDataSource
头文件)
#pragma mark - Initialization
static NSString *const kReuseIdentifier = @"tableViewCellIdentifier";
static NSString *const kIdentifierCellAlias = @"kIdentifierCellAlias";
static NSString *const kSelfDefineCellAlias = @"kSelfDefineCellAlias";
static NSString *const kSectionZeroAlias = @"kSectionZeroAlias";
static NSString *const kSectionOneAlias = @"kSectionOneAlias";
WAMSectionInfo *zero = [WAMSectionInfo infoWithCellInfos:@[
[WAMCellInfo infoWithSelfDefineCell:self.customizedCell alias:kSelfDefineCellAlias]
] alias:kSectionZeroAlias];
WAMSectionInfo *one = [WAMSectionInfo infoWithCellInfos:@[
[WAMCellInfo infoWithReuseIdentifier:kReuseIdentifier title:nil detail:nil alias:kIdentifierCellAlias]
] alias:@"oneSectionAlias"];
self.dataSource = [WAMDataSource dataSourceWithSectionInfos:@[zero, one]];
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataSource.sectionInfos.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.sectionInfos[section].cellInfos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WAMCellInfo *cellInfo = self.dataSource.sectionInfos[indexPath.section].cellInfos[indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfo.identifier forIndexPath:indexPath];
if ([cellInfo.alias isEqualToString:kSelfDefineCellAlias]) {
// do something
} else if ([[self.dataSource indexPathOfCellInfoWithAlias:kIdentifierCellAlias] isEqual:indexPath]) {
// do something
}
.
.
.
return cell;
}
WAMSimpleDataSource 在 WAMSimpleDataSourceTests 目录中包括单元测试。这些测试可以轻松运行。
WAMSimpleDataSource 在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE。