WAMSimpleDataSource,更優雅的編寫靜態UITableView
序
相信做iOS開發的小伙伴們經常會遇到這樣的頁面:
對于這樣的靜態列表我們可以直接用 storyboard 拖一個出來,或者直接用代碼創建。我個人的話會選擇用代碼直接創建,但是之前一直有的問題是沒有較好的數據源表示方式,需要對 indexPath 進行硬編碼,這導致了在 tableView 的代理里面需要進行判斷:
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。
設計思路
在定義 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 在靜態列表中如何能讓代碼看起來更簡潔。
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;
}</code></pre>
總結與缺陷
現在的 WAMDataSource 還沒辦法做到直接作為 tableView 的數據源,這是在今后的更新中會解決的問題。雖然 WAMSimpleDataSource 并沒有減少很多代碼量,但能提升靜態列表中代碼的可讀性以及可維護性,個人覺得還是值得的。