I have an UITableView which shows the list of pdf files (from Documents folder) the user saved.
When I try to remove the only row in the table, it works fine.
But when the rows are more than one, and i try to remove the third row, for example, I get..
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. 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).'
Now, I tried to check whether my array count was > 0, in that case I removed the row, else the indexSet, but without any luck.
This is the code I’m using
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[self.pdfArray objectAtIndex:indexPath.row]]];
if ([fileMgr removeItemAtPath:filePath error:&error] != YES)
{ UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:NSLocalizedString(@"UnableToDeleteFile", @"Unable to delete file: %@"),[error localizedDescription]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease];
[alert show];
}
[self.pdfArray removeObjectAtIndex:indexPath.row];
[self refreshTable];
[tableView endUpdates];
}
[self.tableView reloadData];
}
Where [self refreshTable] shows a view with a label stating that there are no PDF saved:
- (void) refreshTable {
[self fillArray];
[self.tableView reloadData];
if (self.pdfArray.count == 0)
{
[UIView animateWithDuration:1.0f animations:^{
noPDF.view.alpha = 1;
}];
self.navigationItem.rightBarButtonItem = nil;
self.tableView.userInteractionEnabled = NO;
}
else
{
noPDF.view.alpha = 0;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.tableView.userInteractionEnabled = YES;
}
}
Now, I can’t actually get how can I solve this, so, any help highly appreciated!
Wow, perfect timing.
Soon after i wrote this answer, I made some adjustments to the method
[self refreshTable],moving the [self fillArray] method toviewDidLoadand [self refreshTable] toviewDidAppearSo now I have
}
And the row deletion works perfectly.
Thanks for your time, though.