微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NSMutableDictionary 不更新 tableView

如何解决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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。