序 相信做iOS开发的小伙伴们经常会遇到这样的页面: 对于这样的静态列表我们可以直接用 storyboard

移动开发 iOS
现在的 WAMDataSource 还没办法做到直接作为 tableView 的数据源,这是在今后的更新中会解决的问题。虽然 WAMSimpleDataSource 并没有减少很多代码量,但能提升静态列表中代码的可读性以及可维护性,个人觉得还是值得的。

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

 

 

 

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

  1. if (indexPath.section == 0) { 
  2.     if (indexPath.row == 0) { // email 
  3.         // do something 
  4.     } else if (indexPath.row == 1) { // phone 
  5.         // do something 
  6.     } 
  7. else if (indexPath.section == 1) { 
  8.     // do something 
  9.  

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

设计思路

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

WAMCellInfo

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

WAMSectionInfo

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

WAMDataSource

WAMDataSource 是所有 WAMSectionInfo 的容器,同样提供了添加,删除,替换,以及基于 alias 对 WAMSectionInfo 的索引方法。

Demo

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

  1. static NSString *const kReuseIdentifier     = @"tableViewCellIdentifier"
  2. static NSString *const kIdentifierCellAlias = @"kIdentifierCellAlias"
  3. static NSString *const kSelfDefineCellAlias = @"kSelfDefineCellAlias"
  4.  
  5. static NSString *const kSectionZeroAlias = @"kSectionZeroAlias"
  6. static NSString *const kSectionOneAlias  = @"kSectionOneAlias"
  7.  
  8. #pragma mark - Initialization 
  9.  
  10. // section info初始化 
  11. WAMSectionInfo *zero = [WAMSectionInfo infoWithCellInfos:@[] alias:kSectionZeroAlias]; 
  12. // 添加操作,cell info初始化 
  13. [zero appendingCellInfo:[WAMCellInfo infoWithSelfDefineCell:self.customizedCell alias:kSelfDefineCellAlias]]; 
  14.  
  15. WAMSectionInfo *one = [WAMSectionInfo infoWithCellInfos:@[ 
  16.         [WAMCellInfo infoWithReuseIdentifier:kReuseIdentifier title:nil detail:nil alias:kIdentifierCellAlias] 
  17.     ] alias:@"oneSectionAlias"]; 
  18.  
  19. // data source初始化 
  20. self.dataSource = [WAMDataSource dataSourceWithSectionInfos:@[zero, one]]; 
  21.  
  22. #pragma mark - UITableViewDataSource 
  23.  
  24. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
  25.     return self.dataSource.sectionInfos.count
  26.  
  27. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
  28.     return self.dataSource.sectionInfos[section].cellInfos.count
  29.  
  30. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
  31.     WAMCellInfo *cellInfo = self.dataSource.sectionInfos[indexPath.section].cellInfos[indexPath.row]; 
  32.     __kindof UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfo.identifier forIndexPath:indexPath]; 
  33.  
  34.     // 根据不同的alias进行不同的操作 
  35.     if ([cellInfo.alias isEqualToString:kSelfDefineCellAlias]) { 
  36.         // do something 
  37.     } else if ([[cellInfo.alias isEqualToString:kIdentifierCellAlias]) { 
  38.         // do something 
  39.     } 
  40.     . 
  41.     . 
  42.     . 
  43.  
  44.     return cell; 
  45.  
  46. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
  47.     return self.dataSource.sectionInfos[section].sectionHeaderHeight; 
  48.  
  49. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { 
  50.     return self.dataSource.sectionInfos[section].sectionFooterHeight; 
  51.  

总结与缺陷

现在的 WAMDataSource 还没办法做到直接作为 tableView 的数据源,这是在今后的更新中会解决的问题。虽然 WAMSimpleDataSource 并没有减少很多代码量,但能提升静态列表中代码的可读性以及可维护性,个人觉得还是值得的。 

责任编辑:庞桂玉 来源: segmentfault
相关推荐

2019-02-14 13:30:54

内存泄露运维

2014-01-22 14:27:25

科技创业者人品

2013-12-19 10:20:19

2021-12-30 18:57:49

计算

2013-12-27 09:46:40

Windows 9Windows 9桌面

2013-08-09 10:37:31

代码数据

2013-07-22 11:06:37

2021-06-16 09:10:29

APP开发AndroidiOS

2023-03-27 00:06:12

2014-11-26 10:47:46

虚拟现实苹果

2013-08-05 14:34:46

2021-09-09 06:55:44

Python浏览器程序

2015-06-15 10:50:58

iOS 9苹果应用开发商

2015-05-19 14:30:48

加密视频加密亿赛通

2014-06-06 13:49:01

程序员项目经理

2021-10-17 23:46:06

Go项目版本号

2018-10-16 10:29:40

C语言编程错误

2012-10-12 10:13:26

eclips代码编写Editplus

2019-06-28 14:42:45

Vue应用服务器VueJs

2021-03-08 08:16:30

负载均衡系统流量
点赞
收藏

51CTO技术栈公众号