WAMSimpleDataSource,更优雅的编写静态UITableView

嘛,开始以前先双手奉上Github项目地址,欢迎各位大佬star:
https://github.com/WAMaker/WA...git

相信作iOS开发的小伙伴们常常会遇到这样的页面:github

帐号和密码
消息推送设置

对于这样的静态列表咱们能够直接用 storyboard 拖一个出来,或者直接用代码建立。我我的的话会选择用代码直接建立,可是以前一直有的问题是没有较好的数据源表示方式,须要对 indexPath 进行硬编码,这致使了在 tableView 的代理里面须要进行判断:app

if (indexPath.section == 0) {
    if (indexPath.row == 0) { // email
        // do something
    } else if (indexPath.row == 1) { // phone
        // do something
    }
} else if (indexPath.section == 1) {
    // do something
}

稍微好点的会在相关的判断边上作注释,可是这样写依然容易在日后(产品)调整顺序时调整了一个地方而忘记另外的,总的来讲就是代码不够优雅。基于这样的背景,在尝试了各类方式以后,产生了一个可行的解决方案 —— WAMSimpleDataSource。ide

设计思路

在定义 WAMCellInfoWAMSectionInfo 两个类时我选择引入别名( alias )来解决 indexPath 的硬编码问题(可能有人会说alias也是硬编码,但这样作提高了代码的可读性=0=)。编码

WAMCellInfo

WAMCellInfo 为 cell 的建立提供了最基本的信息,如 reuseIdentifier ,title,detail。用户也能传入自定义的 cell 而没必要担忧循环引用的问题。spa

WAMSectionInfo

WAMSectionInfo 做为 WAMCellInfo 的容器,提供了添加删除替换,以及基于 alias 对 WAMCellInfoWAMCellInfo索引方法。设计

WAMDataSource

WAMDataSource 是全部 WAMSectionInfo 的容器,一样提供了添加删除替换,以及基于 alias 对 WAMSectionInfo索引方法。代理

Demo

让咱们就以一个简单的 demo 看下 WAMSimpleDataSource 在静态列表中如何能让代码看起来更简洁。code

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";

#pragma mark - Initialization

// section info初始化
WAMSectionInfo *zero = [WAMSectionInfo infoWithCellInfos:@[] alias:kSectionZeroAlias];
// 添加操做,cell info初始化
[zero appendingCellInfo:[WAMCellInfo infoWithSelfDefineCell:self.customizedCell alias:kSelfDefineCellAlias]];

WAMSectionInfo *one = [WAMSectionInfo infoWithCellInfos:@[
        [WAMCellInfo infoWithReuseIdentifier:kReuseIdentifier title:nil detail:nil alias:kIdentifierCellAlias]
    ] alias:@"oneSectionAlias"];

// data source初始化
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];
    __kindof UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfo.identifier forIndexPath:indexPath];

    // 根据不一样的alias进行不一样的操做
    if ([cellInfo.alias isEqualToString:kSelfDefineCellAlias]) {
        // do something
    } else if ([[cellInfo.alias isEqualToString:kIdentifierCellAlias]) {
        // do something
    }
    .
    .
    .

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.dataSource.sectionInfos[section].sectionHeaderHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.dataSource.sectionInfos[section].sectionFooterHeight;
}

总结与缺陷

如今的 WAMDataSource 还没办法作到直接做为 tableView 的数据源,这是在从此的更新中会解决的问题。虽然 WAMSimpleDataSource 并无减小不少代码量,但能提高静态列表中代码的可读性以及可维护性,我的以为仍是值得的。blog

但愿各位会喜欢,有问题能够留言或者在 Github 上给我提 PR,很是感谢。

GitHub repo:https://github.com/WAMaker/WA...

相关文章
相关标签/搜索