iosarraysobjective-cnsmutablearraynsmutabledictionary

NSMutableDictionary not updating the tableView


I have this this code to edit/delete car

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

And i have this delegate to update the tableview

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

But it seems that even after showing the selected index in the console it doesnt change/or update the tableview.enter image description here


Solution

  • You are just modifying the value of a local variable. This will solve your problem.

        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;