对于静态列表,我们可以直接用storyboard拖一个出来,或者直接用代码创建.个人会选择直接用代码创建,但是之前遇到的问题是没有更好的数据源表示,indexPath需要硬编码,导致需要在tableView的proxy中进行判断:if(indexPath.section==0){if(indexPath.row==0){//email//dosomething}elseif(indexPath.row==1){//phone//dosomething}}elseif(indexPath.section==1){//dosomething}好一点的会在相关判断旁边做注释,但是以后这样写(product)还是容易调整一个地方忘了另一个.总的来说,代码不够优雅。基于这样的背景,在尝试了各种方法之后,产生了一个可行的方案——WAMSimpleDataSource。设计思路在定义WAMCellInfo和WAMSectionInfo这两个类时,我选择引入一个别名(alias)来解决indexPath的硬编码问题(可能有人会说别名也是硬编码的,但这提高了类的可读性代码=0=)。WAMCellInfoWAMCellInfo提供了cell创建的最基本信息,比如reuseIdentifier、title、detail。用户也可以传入自定义单元格而不用担心循环引用。WAMSectionInfoWAMSectionInfo作为WAMCellInfo的容器,提供了WAMCellInfo和WAMCellInfo基于别名的增删改查方法。WAMDataSourceWAMDataSource是所有WAMSectionInfo的容器,也提供了WAMSectionInfo基于别名的增删改查索引方法。Demo下面通过一个简单的demo,看看WAMSimpleDataSource是如何让静态列表中的代码看起来更加简洁的。staticnsString*constkReuseIdentifier=@“tableViewCellIdentifier”;staticnsstring*constkIdentifierCellalias=@“kidentifierCellalias”;statkskellalias=@staticnsstring*constkskellias=@“/sectioninfo初始化WAMSectionInfo*zero=[WAMSectionInfoinfoWithCellInfos:@[]alias:kSectionZeroAlias];//添加操作,cellinfo初始化[zeroappendingCellInfo:[WAMCellInfoinfoWithSelfDefineCell:self.customizedCellalias:kSelfDefineCellAlias]];WAMSectionInfo*one=[WAMSectionInfoinfoWithCellInfos:@[[WAMCellInfoinfoWithReuseIdentifier:kReuseIdentifiertitle:nildetail:nilalias:kIdentifierCellAlias]]alias:@"oneSectionAlias"];//数据源初始化self.dataSource=[WAMDataSourcedataSourceWithSectionInfos:@[zero,one]];#pragmamark-UITableViewDataSource-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{returnsself.dataSource.sectionInfos.count;}-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{returnsself.dataSource.sectionInfos[section].cellInfos.count;}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{WAMCellInfo*cellInfo=self.dataSource.sectionInfos[indexPath.section].cellInfos[indexPath.row];__kindofUITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:cellInfo.identifierforIndexPath:indexPath];//根据不同的别名进行不同的操作if([cellInfo.aliasisEqualToString:kSelfDefineCellAlias]){//dosomething}elseif([[cellInfo.aliasisEqualToString:kIdentifierCellAlias]){//dosomething}...returncell;}-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section{returnsself.dataSource.sectionInfos[section].sectionHeaderHeight;}-(CGFloat)tableView:(UITableView*)tableViewheightForFooterInSection:(NSInteger)section{returnsself.dataSource.sectionInfos[section].sectionFooterHeight;}总结及缺陷目前WAMDataSource不能直接作为tableView的数据源,会在以后的更新中解决。虽然WAMSimpleDataSource并没有减少很多代码,但是它可以提高static列表中代码的可读性和可维护性在我个人看来是值得的。
