2012-01-21 17 views
1

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]); 
    } 
} 
+0

これをチェックしてみてください - あなたの問題である可能性があります: http://stackoverflow.com/questions/5043729/core-data-example-invalid-update-invalid-number-of-rows-in-セクション0 – carbonbasednerd

+0

'// 3。 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];成功が真である場合に限り、ステップ6の内部に入れてみます。あなたのモデルと行はテーブルビューで一致していません –

答えて

3

あなたは3つの段階でテーブルを掘り下げる必要があります。

1)モデルを扱う

[documentURLs removeObjectAtIndex:row]; 

2)あなたのテーブルのアニメーション

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

3とのディール)あなたがエラーを持っている可能性があるため、(多分それは前に行うことができるファイルの更新に対処ファイルの削除)の

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

    NSURL *urlToDelete = nil; 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     NSInteger row = [indexPath row]; 
     urlToDelete = [documentURLs objectAtIndex:row]; 
     [documentURLs removeObjectAtIndex:row]; // you need to update your model 

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     // do stuff to update the file here 
    } 
} 

または

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

    NSURL *urlToDelete = nil; 

    if(editingStyle == UITableViewCellEditingStyleDelete) { 
     NSInteger row = [indexPath row]; 
     urlToDelete = [documentURLs objectAtIndex:row]; 

     // do stuff to update the file here... 

     if(success) 
     { 
      [documentURLs removeObjectAtIndex:row]; // you need to update your model 

      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
    } 
} 

注。モデルからアイテムを削除し、モデル数が0に等しい場合は注意が必要です。その場合は、セクション全体を削除する必要があります。

希望します。

P.S. XCodeなしで書いたのでコードを確認してください

+0

それはトリックでした、あなたの2番目の提案は私を正しい方向に向けると指摘しました。助けてくれてありがとう、あなたは私の一日を作った! – Dan

0

はそんなに一例として

[self.wallsTable beginUpdates]; //<<<<<<<<<<<<<<<<<<<<<<<<< 
NSArray *paths = [NSArray arrayWithObject: deleteView.cellPath]; 
[self.wallsTable deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop]; 
[[self.wallEntries objectAtIndex:[deleteView.cellPath section]] removeObjectAtIndex:[deleteView.cellPath row]]; 
[self.wallsTable endUpdates]; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

のように開始と終了の更新で削除をラップする必要がありますのでから取り除く必要があるテーブルとこれらの中の細胞を供給しているものから

+0

@ carbonbasednerd - 私はすでに配列に格納していますが、提案に感謝します。 – Dan

+0

@Flex_Addicted - 投稿する前にそのアプローチを試みましたが、違いはありませんでしたが、提案に感謝しました。 – Dan

+0

私はあなたの提案を理解しているかどうかわからない、あなたの提案を私のBOOLに適用しようとしましたが、まだ運がありません。最後の行であれば完璧に動作しますが、真ん中にはありません。他のアイデア? – Dan

0

ステップ3はステップ2の内側にある必要があります。ブロックの場合は、editingStyleがdeleteの場合のみ削除してください。 それ以外のデータ構造を調整することなくテーブルから行を削除しています。

//2. If row is deleted, remove it from the list. 
if (editingStyle == UITableViewCellEditingStyleDelete) { 
    NSInteger row = [indexPath row]; 
    urlToDelete = [documentURLs objectAtIndex:row]; 
    [documentURLs removeObjectAtIndex:row];  
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
        withRowAnimation:UITableViewRowAnimationFade]; 
} 
関連する問題