このページを使用して、テーブルにチェックボックス機能を作成しました。
http://www.carrotcoded.com/2012/12/02/uibutton-within-uitableviewcell-checkbox/
これは問題なく動作し、チェックボックスをオンまたはオフにすることができます。次に、作成されたデリゲート "clearCheckedItems" でそれらをすべてクリアします。このメソッドには、チェックされていないアイテムを新しい Glist に追加し、削除されたインデックスの配列を構築するループがあります。
アプローチ 0、「beginUpdates」メソッド: インデックス リストを removeRows メソッドに渡しましたが、「不良セレクター」が原因でクラッシュし、下部に NSLog が表示されます。
アプローチ1、「reloadView」メソッド:チェックされた行を削除し、クラッシュしませんが、「ゴースト」である永続的な「チェックされた」チェックボックスのグラフィックを残し、そのテーブルセルの状態を示しません。
//////////////// start of code //////////////////////////
- (IBAction)clearCheckedItems:(id)sender {
groceryList *myGroceryList = [ groceryList sharedSingleton ];
gList = [[NSMutableArray alloc] init ];
gList = myGroceryList.myGroceryListS ;
int i;
NSMutableArray *checkedOffGlist = [[NSMutableArray alloc] init ];
NSMutableArray *removedIndexArray = [[ NSMutableArray alloc] init ];
for ( i=0 ; i < sizeOfList ; i++) {
groceryItem *current = [ gList objectAtIndex:i ];
if ( current.bought == (bool )NO ) {
[ checkedOffGlist addObject:current ] ;
} else {
[ removedIndexArray addObject:[ NSNumber numberWithInt:i ] ] ;
}
}
myGroceryList.myGroceryListS = checkedOffGlist ;
//* update the table, clear all the check boxes *//
NSString *whichTableUpdate = @"beginUpdates";
//* beginUpdates method *//
if ( [whichTableUpdate isEqualToString:@"beginUpdates" ]) {
[ self.tableView beginUpdates];
[ self.tableView deleteRowsAtIndexPaths:
[NSArray arrayWithObject:removedIndexArray ]
withRowAnimation:UITableViewRowAnimationAutomatic ];
[ self.tableView endUpdates];
// *** fails with message below *** //
} else {
//* reloadView method *//
[ self.tableView reloadData ];
// *** does not crash, but leaves behind a persistent "checked" box icon.
}
}
2013-06-18 09:32:32.843 shoppingList[5730:c07] * キャッチされていない例外 'NSInvalidArgumentException' が原因でアプリを終了しています。
..cb30 は、削除されたインデックス「removedIndexArray」のリストへのポインタです。
では、どうすれば正しいインデックス リストを行リムーバーに取得できますか、それとも行削除メソッドを間違って実行していますか?