1.最开始工作时配置tableView数据基本流程是:数据数组-数据展示,这期间没有根据数组做任何的配置,这样导致了在tableView代理方法里面产生了大量的代码:
-
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (locationType == LocationFail || locationType == LocationIng) { if (self.locationModel.communityArray.count > 0) { return 3; }else{ return 2; } }else if (locationType == LocationSuccess){ return self.communityArray.count; }else{ return self.searchArray.count; } } 像上面方法里面的if判断在接下来的几个代理方法里面基本都要重写一遍,十分耗费精力,还容易出错。
2.后面根据数组中模型的种类(即会有多少种单元格),来配置单元格
self.cellIdentifiers = [[NSMutableArray alloc] init];
- (void)addCellIdentifier { [self.cellIdentifiers removeAllObjects]; if ([self.bannerArray count]>0) { [self.cellIdentifiers addObject:@"CFHomeBannerCell"]; } if ([self.newsArray count] >0) { [self.cellIdentifiers addObject:@"CFNewsCell"]; } if ([self.productArray count] > 0) { [self.cellIdentifiers addObject:@"CFNewHomefouncionCell"]; } if ([self.hotArray count] > 0) { [self.cellIdentifiers addObject:@"CFNewHomeHotCell"]; } }
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString * identifier = self.cellIdentifiers[indexPath.section]; if ( [identifier isEqualToString:@"CFHomeBannerCell"] ) { return 150; }else if ([identifier isEqualToString:@"CFNewsCell"]) { return 44; }else if ([identifier isEqualToString:@"CFNewHomefouncionCell"]) { return 130; } return 0.1; }
这种根据cell来配置tableView更加简约,如果采用数据模型来配置的话,就有点乱了