偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

 

 

 

對于這樣的靜態(tài)列表我們可以直接用 storyboard 拖一個出來,或者直接用代碼創(chuàng)建。我個人的話會選擇用代碼直接創(chuàng)建,但是之前一直有的問題是沒有較好的數(shù)據(jù)源表示方式,需要對 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.  

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

設(shè)計思路

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

WAMCellInfo

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

WAMSectionInfo

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

WAMDataSource

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

Demo

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

  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 還沒辦法做到直接作為 tableView 的數(shù)據(jù)源,這是在今后的更新中會解決的問題。雖然 WAMSimpleDataSource 并沒有減少很多代碼量,但能提升靜態(tài)列表中代碼的可讀性以及可維護(hù)性,個人覺得還是值得的。 

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

2019-02-14 13:30:54

內(nèi)存泄露運維

2021-12-30 18:57:49

計算

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

2014-11-26 10:47:46

虛擬現(xiàn)實蘋果

2012-10-11 09:46:20

2015-05-19 14:30:48

加密視頻加密億賽通

2021-06-16 09:10:29

APP開發(fā)AndroidiOS

2013-08-05 14:34:46

2021-09-09 06:55:44

Python瀏覽器程序

2014-06-06 13:49:01

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

2021-10-17 23:46:06

Go項目版本號

2018-10-16 10:29:40

C語言編程錯誤

2015-12-10 10:14:28

物聯(lián)網(wǎng)物聯(lián)網(wǎng)技術(shù)
點贊
收藏

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