NSMutableDictionary 不更新 tableView

问题描述

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

- (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];
} 

我有这个委托来更新 tableview

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

但似乎即使在控制台中显示所选索引后,它也不会更改/或更新 tableview。

enter image description here

解决方法

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

    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;