NSMutableDictionary 未更新 tableView

NSMutableDictionary not updating the tableView

提问人:Jann 提问时间:2/16/2021 最后编辑:Jann 更新时间:2/16/2021 访问量:30

问:

我有这个代码来编辑/删除汽车

- (void)dismissViewWithIndex:(NSInteger)index selectedVehicle:(NSInteger)selectedVehicle toDelete:(BOOL)toDelete {
    if (toDelete && [self.cars count] > 0) {
        [self.cars removeObjectAtIndex:(index-1)];
    } else {

        NSMutableDictionary *editCar =[NSMutableDictionary dictionaryWithDictionary:[self.cars objectAtIndex:index-1]];
        editCar[@"vehicleType"] = selectedVehicle == 0 ? @"SmallTruck" : @"BigTruck";
    }
    [self.tableView reloadData];
} 

我有这个委托来更新表视图

#pragma mark - Delegate
- (void)updateSelectedVehicle:(BOOL)toDelete {
    [self.delegate dismissViewWithIndex:(int)self.selectedCarIndex selectedVehicle:self.vehiclePreviousIndexPath.item toDelete:toDelete];
    [self.navigationController popViewControllerAnimated:YES];
}

但似乎即使在控制台中显示所选索引后,它也不会更改/或更新表视图。enter image description here

ios 数组 objective-c nsmutablearray nsmutabledictionary

评论

0赞 Thilina Chamath Hewagama 2/16/2021
你能在'dismissViewWithIndex'方法中放一个断点,看看执行了哪些部分吗?
0赞 Jann 2/16/2021
@ThilinaChaminHewagama我为 NSMutableDictionary *editCar , editCar 和 Tableview Reload 放置断点,它会转到 editCar,然后转到 tableview Reload

答:

1赞 Thilina Chamath Hewagama 2/16/2021 #1

您只是在修改局部变量的值。这将解决您的问题。

    NSMutableArray *cars = [[NSMutableArray alloc] initWithArray:self.cars];
    NSMutableDictionary *editCar =[NSMutableDictionary dictionaryWithDictionary:[self.cars objectAtIndex:index-1]];
    editCar[@"vehicleType"] = selectedVehicle == 0 ? @"SmallTruck" : @"BigTruck";
    [cars replaceObjectAtIndex:index-1 withObject:editCar];
    self.cars = cars;