tableView列表数据处理


之前tableView的数据是直接从plist里面拉取的,可是现在是要我从接口获取数据,想想挺简单,几分钟就可以搞定,但是现实是数据只从“箱式货车”-“平板车”,并没有全部数据的存在,代码是这样的:

//获取列表数据

- (void)getTruckTypeListCommand {
    __weak typeof(self) weakSelf = self;
    BFSChooseTruckTypeCommand *typeCmd = [[BFSChooseTruckTypeCommand alloc]init];
    typeCmd.userID = [BFSUserSystem userSystem].mySelf.userID;
    [typeCmd setResultHandler:^(id data) {
        [weakSelf hideHud];
        weakSelf.groupList = ((BFSBackData *)data).data;
        [self dealWithcarTypeArray];
    } failure:^(NSError *error) {
        [weakSelf hideHud];
    }];
    [[BFSDataCenter sharedInstance] execute:typeCmd];
}

//遍历列表数据
- (void)dealWithcarTypeArray {
    
    NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:self.groupList.count];
    for (BFSLogisticsTruckTypeGroup *group in self.groupList) {
        [array addObject:group];
    }
    self.carTypeArray = array;
    [self.carTypeTableView reloadData];
}



tableView数据源代理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (tableView == self.carTypeTableView) {
     static   NSString *cellID = @"carTypeTableView";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        
        if( [self.carTypeArray[indexPath.row] isKindOfClass:BFSLogisticsTruckTypeGroup.class])
        {
            BFSLogisticsTruckTypeGroup *model = self.carTypeArray[indexPath.row];
            cell.textLabel.text = model.truckName;
        }
        return cell;
    }
}

- (void)carTypeTableViewDidSelectAtIndexPath:(NSIndexPath *)indexPath {
    BFSLogisticsTruckTypeGroup *model = self.carTypeArray[indexPath.row];
    self.truckType = [model.truckId stringValue];
    self.pageIndex = 0;
    [self.carTypeBtn setTitle:model.truckName forState:UIControlStateNormal];
    [self getCanRobOrderListwithtRuckType];
    [self showCarTypeSelect: NO];
}

看起来几乎完美,但是运行效果是这样的看起来有些让人伤心,数据都出来了没错,可是这个”全部“我到底该怎么加上去呢,,并且选中全部的时候还要加载全部的列表数据,到底这个全部在哪里加,怎么个加法,对于我这个菜鸟来说真的是让我头疼了不少时间,知道周一早上我在忽然想到,
 self.carTypeArray<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">不是装的不是 </span><span style="font-family: Arial, Helvetica, sans-serif;">BFSLogisticsTruckTypeGroup这样的模型数据么,这样我只要在 </span><span style="font-family: Arial, Helvetica, sans-serif;">self.carTypeArray</span><span style="font-family: Arial, Helvetica, sans-serif;">数组的最前面加上一个</span><span style="font-family: Arial, Helvetica, sans-serif;">BFSLogisticsTruckTypeGroup并且把他的truckName设置为”全部“,而此时这个加入的元素里面只有一个truckName,在</span><span style="font-family: Arial, Helvetica, sans-serif;">carTypeTableViewDidSelectAtIndexPath:(NSIndexPath *)indexPath,</span><span style="font-family: Arial, Helvetica, sans-serif;">truckType自然就为空了,而truckType为空的时候默认就是请求全部数据的。</span>
<span style="font-family: Arial, Helvetica, sans-serif;">此时,我加了这两行代码</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="objc">- (void)dealWithcarTypeArray {
    
    NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:self.groupList.count];
    for (BFSLogisticsTruckTypeGroup *group in self.groupList) {
        [array addObject:group];
    }
    
 <span style="color:#ff0000;">   BFSLogisticsTruckTypeGroup *group = [[BFSLogisticsTruckTypeGroup alloc]init];
    [array insertObject:group atIndex:0];
    group.truckName = @"全部";</span>
    self.carTypeArray = array;
    [self.carTypeTableView reloadData];
}

对头,就是这样简单的三行代码解决了我的问题,虽然很简单,但我还是纠结了这么久,大概是我大脑不够用吧,所以还是记录下来比较好。

 










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值