I am getting this error when trying to delete a row out of a UITableView. If I delete the last row in the tableview is does not error out and everything works fine but any other row throws an exception. Can someone tell me what I am doing wrong here? Any help would be much appreciated!
Error:
'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Code
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//1. clear existing URL's
NSURL *urlToDelete = nil;
//2. If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger row = [indexPath row];
urlToDelete = [documentURLs objectAtIndex:row];
//[documentURLs removeObjectAtIndex:row];
}
//3. update the tableview on the fly without reloading the data
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
//4. get the location of the files
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocumentsDirectory = [paths objectAtIndex:0];
//5.setup file manager
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:pathToDocumentsDirectory];
NSLog(@"Path to file: %@", pathToDocumentsDirectory);
NSLog(@"File exists: %d", fileExists);
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:pathToDocumentsDirectory]);
//6. remove if matches
if (urlToDelete) {
BOOL success = [fileManager removeItemAtURL:urlToDelete error:&error];
if (!success) NSLog(@"Error: %@", [error localizedDescription]);
}
}