2010-11-22 4 views
1

commitEditingStyleを使用してUITableViewから行を削除すると、次のエラーメッセージが表示され、アプリケーションがクラッシュします。しかし、私はメッセージに従って部3から矛盾を削除していている奇妙なことは、セクション4行を削除するとクラッシュする - セクションの不一致が更新されない

からである中*アサーションの失敗 - [のUITableView _endCellAnimationsWithContext:]、/SourceCache/UIKit_Sim/UIKit-1262.60。 3/UITableView.m:920 2010-11-22 19:56:44.789 bCab [23049:207] *キャッチされていない例外 'NSInternalInconsistencyException'のためアプリを終了します。理由:無効な更新:セクション4の行数が無効です。更新(1)の後の既存のセクションに含まれる行の数は、更新前のそのセクションに含まれる行の数(0)、そのセクションに挿入または削除された行の数をプラスまたはマイナスしたもの、0削除されました)。 '

データソースでは、セクション3の行数に応じてセクション4を更新します。セクション3から行が削除されると、セクション4の行数は0から1になります。 。これを避ける方法はありませんか?

いずれのポインタも高く評価されます。おかげさまで

UPDATE

numberOfSectionsInTableView - (NSInteger)numberOfSectionsInTableView:(のUITableView *)のtableView { リターン6。
}

numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
bCabAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 

    if (section == 0) {  // First name, last name 
     return 2; 
    } 
    else if (section == 1) { // Password 
     return 2; 
    } 
    else if (section == 2) { // Mobile, DOB , Gender 
     return 3; 
    } 
    else if (section == 3) { // credit cards 
     return [creditCards count]; 
    } 
    else if (section == 4) { // Add credit card 
     if ([creditCards count] >= 3) { 
      return 0; 
     } 
     else { 
      return 1; 
     } 
    } 
    else if (section == 5) { 
     return 0; 
    } 

    return 0; 

}

commitEditingStyle

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     if (indexPath.section == 3) { 
     // Delete the row from the data source 
     NSLog(@"%d %d", indexPath.section, indexPath.row); 
     [creditCards removeObjectAtIndex:indexPath.row]; 

     // Delete from backend 

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

     //[tableView reloadData]; 
     } 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 

}

私は同じ結果で[tableView reloadData]を試してみました。

ありがとうございました!

+0

は、あなたのnumberOfSectionsInTableViewとのtableViewを投稿することができますか? – MathieuF

+0

行を削除するコードを投稿できますか? –

答えて

3

行が 部3から削除されると、私は セクション内の行の数に応じてセクション4 を更新3、セクション4 の行数は、0から1へ移行これは思えます問題を にしてください。 を避ける方法はありますか?

deleteRowsAtIndexPaths:withAnimationを使用すると、指定したインデックスパスを持つ行だけがデータソースによって削除されることが保証されます。あなたのケースでは、テーブルに行を挿入しているため、データソースの状態は、テーブルビューに必要なものではありません。また、あなたのような何かをしなければならないセクション4に行を挿入する必要セクション3の行を削除する場合

:numberOfRowsInSection方法プラザ:

[self.tableView beginUpdates]; 
[self.tableView [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView insertRowsAtIndexPath:[NSArray arrayWithObject:indexPathForInsertedRow] withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView endUpdates]; 
+0

それは完璧に機能しました!ありがとう、トン.. – Prasanna

0

セクション3の行が削除されたときにセクション4に新しい行を追加するにはどうすればよいですか?
良い解決策ではありませんが、行が削除されたときにreloadDataを作成できます。

+0

システム内のクレジットカードの最大数は3です.3の場合、クレジットカードの追加セルは必要ありません。そうでなければ、私はそれを持っています。 reloadDataを試しましたが、効果はありません。 ご返信ありがとうございます。 – Prasanna

1

セルを削除する前に、creditCardsで対応するオブジェクトを削除しますか?データソースで

+0

はい、対応する配列から削除しています。 commitEditingStyleコードで質問を更新しました。 – Prasanna

+0

行を削除する前にデータを再読み込みしてみますか? – MathieuF

+0

もう一度クラッシュしますが、メッセージは若干異なります - キャッチされていない例外 'NSInternalInconsistencyException'のためアプリを終了しています、理由: '無効な更新:セクション3の行数が無効です。 (3)は、更新(3)前にそのセクションに含まれる行の数に、そのセクションに挿入または削除された行の数をプラスまたはマイナスします(挿入された0、削除された1)。 – Prasanna

関連する問題