序 相信做iOS開發(fā)的小伙伴們經(jīng)常會(huì)遇到這樣的頁(yè)面: 對(duì)于這樣的靜態(tài)列表我們可以直接用 storyboard
序
相信做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)行判斷:
- 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
- }
稍微好點(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)潔。
- 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];
- // 根據(jù)不同的alias進(jìn)行不同的操作
- 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;
- }
總結(jié)與缺陷
現(xiàn)在的 WAMDataSource 還沒(méi)辦法做到直接作為 tableView 的數(shù)據(jù)源,這是在今后的更新中會(huì)解決的問(wèn)題。雖然 WAMSimpleDataSource 并沒(méi)有減少很多代碼量,但能提升靜態(tài)列表中代碼的可讀性以及可維護(hù)性,個(gè)人覺得還是值得的。