kevnm67
kevnm67

Reputation: 177

Deleting from fetchedresultsController error no section at index

I've been trying to figure this out for a while and have not gotten anywhere. I'd love some help.
Im using a table with 2 fetches. Only 1, which is in the last section of the table, can be edited. When I delete an item the app crashes saying there is no section at index. I've read a few responses to others similar question but still can't fix it.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[fetchedResultsController sections] count]+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section < fetchedResultsController.sections.count) {
        return [[fetchedResultsController.sections objectAtIndex:section]     numberOfObjects];
    }
    else {
        return fetchedResultsControllerCustomWOD.fetchedObjects.count;
    }
}

And for the commitEditing method:

- (void)tableView:(UITableView *)tableView commitEditingStyle:    
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObjectContext *context = [self managedObjectContext];
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete object from database
        [context deleteObject:[[fetchedResultsControllerCustomFR     objectAtIndexPath:indexPath] objectAtIndex:indexPath.row]];

        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }

        // Remove device from table view
        [[fetchedResultsControllerCustomFR objectAtIndexPath:indexPath]     removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]     withRowAnimation:UITableViewRowAnimationFade];
    }
}

Upvotes: 0

Views: 816

Answers (1)

random
random

Reputation: 8608

If I understand all your fetch controllers right you are:

Deleting the object from CoreData once here:

 // Delete object from database
        [context deleteObject:[[fetchedResultsControllerCustomFR     objectAtIndexPath:indexPath] objectAtIndex:indexPath.row]];

Then saving the context:

NSError *error = nil;
if (![context save:&error]) {
    NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
    return;
}

Which in turn alerts fetchedResultsControllerCustomFR that an item was deleted.

Which you then try to delete from fetchedResultsControllerCustomFR again:

// Remove device from table view
        [[fetchedResultsControllerCustomFR objectAtIndexPath:indexPath]     removeObjectAtIndex:indexPath.row];

Which should already know it was gone. Given that you have your fetch controller delegates setup.

EDIT:

Wrap the delete call in -begin and -end calls:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

Upvotes: 1

Related Questions