1.对于 NumberOfSection NumberOfRowInSection,我们可以设置为其最多数,仅仅需要用两个方法来确定是否显示TableView.
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _shopDetailModel ? 8 : 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return 3;
}
if (section == 1) {
return 3;
}
if (section == 3) {
return 4;
}
if (section == 4) {//优惠信息
return self.shopDetailModel.couponInfo.count + 2;
}
if (section == 7) {
return 2;
}
return 1;
}
2.通过每个row的高度来让cell显示与否。也就是说逻辑写在heightForCell这边,集中处理数据逻辑。
case 1://预约
{
if (_shopDetailModel && (_shopDetailModel.service & MWShopServiceYuDing))
{
if(indexPath.row == 0){
height = 45;
}else if(indexPath.row == 1){
if (_shopDetailModel.ydInfo) {
height = 105;
}
}else if(indexPath.row == 2){
if (_shopDetailModel.dingdanInfo) {
height = 68;
}
}
}
}
break;
3.在写cellforIndexPath时,就是对应高度是0,它还是会调用cellforindexpath方法,所以heightforcell和cellforindexpath逻辑是一样的,要注意用空cell处理else情况。
if (_shopDetailModel && (_shopDetailModel.service & MWShopServiceYuDing))
{
if(indexPath.row == 0){
cell = [tableView dequeueReusableCellWithIdentifier:kImgLabelImgCellIdentifier];
if (!cell) {
cell = [[AccessoryDefaultTableCell alloc]initWithReuseIdentifier:kImgLabelImgCellIdentifier];
}
[self configureYuyueCell:(AccessoryDefaultTableCell *)cell atIndexPath:indexPath];
}else if(indexPath.row == 1){
cell = [tableView dequeueReusableCellWithIdentifier:kYDBriefCellIdentifier];
if (!cell) {
cell = [[YDBriefCell alloc]initWithReuseIdentifier:kYDBriefCellIdentifier];
}
[self configureYuyueInfoCell:(YDBriefCell *)cell atIndexPath:indexPath];
}else if(indexPath.row == 2){
cell = [tableView dequeueReusableCellWithIdentifier:kYDDingDancellIdentifier];
if (!cell) {
cell = [[YDDingDanCell alloc]initWithReuseIdentifier:kYDDingDancellIdentifier];
}
[self configureYDDingDanCell:(YDDingDanCell *)cell atIndexPath:indexPath];
}
}else{
cell = [tableView dequeueReusableCellWithIdentifier:kEmptyIdentifier forIndexPath:indexPath];
}
4.如果按照2 ,3步骤写,cellfor代码会比较乱繁杂,也可抽出部分逻辑判断到configcell中,如下,这样cellfor中就可以不用判断此逻辑。如果有更好的建议,可以讨论一下。//配置预约时间点行
- (void)configureYuyueInfoCell:(YDBriefCell *)cell atIndexPath:(NSIndexPath *)indexPath{
if (_shopDetailModel.ydInfo) {
cell.timeCollectionView.dataSource = self;
cell.timeCollectionView.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
5.参考美味的shopdetailviewcontroller