自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

序 相信做iOS開發(fā)的小伙伴們經(jīng)常會(huì)遇到這樣的頁(yè)面: 對(duì)于這樣的靜態(tài)列表我們可以直接用 storyboard

移動(dòng)開發(fā) iOS
現(xiàn)在的 WAMDataSource 還沒(méi)辦法做到直接作為 tableView 的數(shù)據(jù)源,這是在今后的更新中會(huì)解決的問(wèn)題。雖然 WAMSimpleDataSource 并沒(méi)有減少很多代碼量,但能提升靜態(tài)列表中代碼的可讀性以及可維護(hù)性,個(gè)人覺得還是值得的。

相信做iOS開發(fā)的小伙伴們經(jīng)常會(huì)遇到這樣的頁(yè)面: 

 

 

 

對(duì)于這樣的靜態(tài)列表我們可以直接用 storyboard 拖一個(gè)出來(lái),或者直接用代碼創(chuàng)建。我個(gè)人的話會(huì)選擇用代碼直接創(chuàng)建,但是之前一直有的問(wèn)題是沒(méi)有較好的數(shù)據(jù)源表示方式,需要對(duì) indexPath 進(jìn)行硬編碼,這導(dǎo)致了在 tableView 的代理里面需要進(jìn)行判斷: 

  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.  

稍微好點(diǎn)的會(huì)在相關(guān)的判斷邊上做注釋,但是這樣寫依然容易在往后(產(chǎn)品)調(diào)整順序時(shí)調(diào)整了一個(gè)地方而忘記另外的,總的來(lái)說(shuō)就是代碼不夠優(yōu)雅。基于這樣的背景,在嘗試了各種方式之后,產(chǎn)生了一個(gè)可行的解決方案 —— WAMSimpleDataSource。

設(shè)計(jì)思路

在定義 WAMCellInfo 和 WAMSectionInfo 兩個(gè)類時(shí)我選擇引入別名( alias )來(lái)解決 indexPath 的硬編碼問(wèn)題(可能有人會(huì)說(shuō)alias也是硬編碼,但這樣做提升了代碼的可讀性=0=)。

WAMCellInfo

WAMCellInfo 為 cell 的創(chuàng)建提供了最基本的信息,如 reuseIdentifier ,title,detail。用戶也能傳入自定義的 cell 而不必?fù)?dān)心循環(huán)引用的問(wèn)題。

WAMSectionInfo

WAMSectionInfo 作為 WAMCellInfo 的容器,提供了添加,刪除,替換,以及基于 alias 對(duì) WAMCellInfo 和 WAMCellInfo 的索引方法。

WAMDataSource

WAMDataSource 是所有 WAMSectionInfo 的容器,同樣提供了添加,刪除,替換,以及基于 alias 對(duì) WAMSectionInfo 的索引方法。

Demo

讓我們就以一個(gè)簡(jiǎn)單的 demo 看下 WAMSimpleDataSource 在靜態(tài)列表中如何能讓代碼看起來(lái)更簡(jiǎn)潔。 

  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.     // 根據(jù)不同的alias進(jìn)行不同的操作 
  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.  

總結(jié)與缺陷

現(xiàn)在的 WAMDataSource 還沒(méi)辦法做到直接作為 tableView 的數(shù)據(jù)源,這是在今后的更新中會(huì)解決的問(wèn)題。雖然 WAMSimpleDataSource 并沒(méi)有減少很多代碼量,但能提升靜態(tài)列表中代碼的可讀性以及可維護(hù)性,個(gè)人覺得還是值得的。 

責(zé)任編輯:龐桂玉 來(lái)源: segmentfault
相關(guān)推薦

2019-02-14 13:30:54

內(nèi)存泄露運(yùn)維

2021-12-30 18:57:49

計(jì)算

2014-01-22 14:27:25

科技創(chuàng)業(yè)者人品

2013-12-19 10:20:19

2013-12-27 09:46:40

Windows 9Windows 9桌面

2013-08-09 10:37:31

代碼數(shù)據(jù)

2013-07-22 11:06:37

2015-06-15 10:50:58

iOS 9蘋果應(yīng)用開發(fā)商

2023-03-27 00:06:12

2012-10-12 10:13:26

eclips代碼編寫Editplus

2021-06-16 09:10:29

APP開發(fā)AndroidiOS

2014-11-26 10:47:46

虛擬現(xiàn)實(shí)蘋果

2012-10-11 09:46:20

2015-05-19 14:30:48

加密視頻加密億賽通

2021-09-09 06:55:44

Python瀏覽器程序

2013-08-05 14:34:46

2014-06-06 13:49:01

程序員項(xiàng)目經(jīng)理

2021-10-17 23:46:06

Go項(xiàng)目版本號(hào)

2018-10-16 10:29:40

C語(yǔ)言編程錯(cuò)誤

2021-02-01 13:35:28

微信Python技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)