之前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];
}
- (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];
}
对头,就是这样简单的三行代码解决了我的问题,虽然很简单,但我还是纠结了这么久,大概是我大脑不够用吧,所以还是记录下来比较好。