嘛,開始以前先雙手奉上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
在定義 WAMCellInfo
和 WAMSectionInfo
兩個類時我選擇引入別名( alias )來解決 indexPath 的硬編碼問題(可能有人會說alias也是硬編碼,但這樣作提高了代碼的可讀性=0=)。編碼
WAMCellInfo
爲 cell 的建立提供了最基本的信息,如 reuseIdentifier ,title,detail。用戶也能傳入自定義的 cell 而沒必要擔憂循環引用的問題。spa
WAMSectionInfo
做爲 WAMCellInfo
的容器,提供了添加,刪除,替換,以及基於 alias 對 WAMCellInfo
和 WAMCellInfo
的索引方法。設計
WAMDataSource
是全部 WAMSectionInfo
的容器,一樣提供了添加,刪除,替換,以及基於 alias 對 WAMSectionInfo
的索引方法。代理
讓咱們就以一個簡單的 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...