When editing the UITableView, the app usually crashes with this error after someone presses the "delete" button on the uitableviewcell. This usually happens for the first item in the table view, but has happened to others. I'm sorry for being so vague, I can provide any additional infomation. I'm just very confused on why this happens and why it happens when it does.
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
-(void)viewWillAppear:(BOOL)animated{
[_matchIDS removeAllObjects];
_matchIDS = [[NSMutableArray alloc]init];
_matchIDS = [[NSUserDefaults standardUserDefaults] valueForKey:@"allMatchIDS"];
[self.tableView reloadData];
}
-(void)viewWillDisappear:(BOOL)animated{
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:_matchIDS forKey:@"allMatchIDS"];
[defaults synchronize];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _matchIDS.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = _matchIDS[indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_matchIDS removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}