I currently have a slexpandabletableview (Sections are expandable to display rows)
The table is populated using a fetchedResultsController.
The table is working as it should.
However when I try and remove a row within a section i get a strange response.
The following code is to attempt to delete the row
- (void)didClickOnCellAtIndex:(NSInteger)cellIndex withData:(id)data
{
// Delete the object
NSIndexPath *path = [NSIndexPath indexPathForItem:cellIndex inSection:0];
NSManagedObject *punToDelete = [self.fetchedResultsController objectAtIndexPath:path];
[appDelegate.managedObjectContext deleteObject:punToDelete];
[appDelegate.managedObjectContext save:nil];}
The error message is as follows
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 93 from section 0 which only contains 14 rows before the update'
I have attempted the following
NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
but the same problem comes back but with a different row
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 77 from section 0 which only contains 14 rows before the update'
I think the problem is related to the SLExpandableTableView??
If it helps my fetchresultscontroller method looks like this
-(void)fetchResultsbyEntity :(NSString*)entity sortWithKey:(NSString*)key{
// Initialize Fetch Request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:entity];
// Add Sort Descriptors
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:key ascending:YES]]];
// Initialize Fetched Results Controller
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
// Configure Fetched Results Controller
[self.fetchedResultsController setDelegate:self];
// Perform Fetch
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
if (error) {
NSLog(@"Unable to perform fetch.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
NSLog(@"fetched results contain %i",self.fetchedResultsController.fetchedObjects.count);
}
I get the exact same issues within the swipe to delete method.
I have beginUpdates and endUpdates in the delegate method of NSFetchedResults Controller
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch (type) {
case NSFetchedResultsChangeInsert: {
[self.mainTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
case NSFetchedResultsChangeDelete: {
[self.mainTableView beginUpdates];
[self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.mainTableView endUpdates];
break;
}
case NSFetchedResultsChangeUpdate: {
//[self configureCell:(TSPToDoCell *)[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
NSLog(@"results changed update called");
break;
}
case NSFetchedResultsChangeMove: {
[self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.mainTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
Sorry if i pasted too much code but i wanted to give you a good understanding of whats going on.
How does one remove the uitableview row?
That looks fine (the usual suspect) so that means that you (or this third party framework) is supplying unexpected information to the table view. Specifically being off by that large of a number (77 of 14) probably means the third party code is doing something wrong/improper.
You are reporting back to the table view correctly (although I personally wouldn't put the begin... end... there). Assuming you are not implementing the -numberOfRowsInTableView:
then the probably is in the third party code. If you are implementing -numberOfRowsInTableView:
then show that code, maybe something wonky in there.