UITableViewから行を削除しようとするとこのエラーが発生します。私がテーブルビューの最後の行を削除すると、エラーが出ず、すべて正常に動作しますが、他の行は例外をスローします。誰かが私がここで間違っていることを教えてもらえますか?どんな助けでも大歓迎です!UITableViewの行を削除中にエラーが発生しました
エラー:
'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).'
コード
- (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]);
}
}
これをチェックしてみてください - あなたの問題である可能性があります: http://stackoverflow.com/questions/5043729/core-data-example-invalid-update-invalid-number-of-rows-in-セクション0 – carbonbasednerd
'// 3。 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];成功が真である場合に限り、ステップ6の内部に入れてみます。あなたのモデルと行はテーブルビューで一致していません –