2010-11-25 2 views
1

IコードがこのエラーでクラッシュするようUITableViewRowAnimationFadeのような標準的なアニメーションを使用して私のテーブルから行を削除する場合:削除表の行

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

Iアニメーションライン行の除去を削除した場合/削除は機能しますが、私はもはやクールなApple削除の移行を持っていません。

ここに私のコードです - どんな助けもありがとうございます。この削除方法は、iOS4より前に動作しましたが、何か変更があるかどうかはわかりません。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    // If row is deleted, remove it from the list. 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; 
     MyItem *myItem = [self.getItemsFetchedResultsController objectAtIndexPath:path]; 
     [dataRepository deleteItem:myItem]; 

     // If I comment this line out the delete works but I no longer have the animation 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     [self.tableView reloadData]; 

    } 

} 

データリポジトリ:あなたはNSFetchedResultsControllerDelegateを設定し、コンテキストを保存し、代わってテーブルビューを更新するときにそれが呼び出されますcontroller:didChangeObject:atIndexPath:forChangeType:newIndexPath:を実装している場合、これは可能性のために

-(void)deleteItem:(MyItem *)myItem { 
    [self.managedObjectContext deleteObject:myItem]; 
    [self saveCurrentContext]; 
} 

答えて

0

一つの理由。あなたはこのようなコードがある可能性があります:あなたがエラーを取得するとき

case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

は、一般的に、それはあなたが deleteRowsAtIndexPaths:withRowAnimation:で指定された方法でデータソースを更新していないことを意味します。このメソッドを呼び出すと、その変更を反映するようにデータソースを既に更新していることが保証されます。

+0

おかげで、私はNSFetchedResultsControllerDelegateを実装していない - これは私がに見るべきもののように聞こえます。問題を解決して回答を投稿しました – KSoza

4

私はテーブルの更新前と後に、このコードを使用してこの問題を解決:あなたの答えのための

[tableView beginUpdates]; 
if (editingStyle == UITableViewCellEditingStyleDelete) { 
    ... 
} 
[tableView endUpdates]; 
+0

私はこれをやってみましたが、それでも私のために働いていません。助けてください – amavi