UITableView 使用小结

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值