私はBarButton
アイテムを持っていて、編集モードのときはEdit
、編集モードの場合はDone
です。ユーザーが編集をクリックすると、editpress
メソッドが実行されます(コードを参照)。テーブルからセルを削除する - コード付き - ロジック問題あり
これで、commitEditingStyle
、およびセルとデータ(セルにデータを追加するために使用される配列からの)が削除されます。
問題点:削除をクリックすると、セルがテーブルから正しく削除されます。しかし、細胞をロードするために使用される配列からではありません。私がcommitEditingStyle
にデバッグポイントを置いたとき、私は最初にdeleteをクリックするとそのコードを実行します。第2、第3などのレコードを削除すると、このコードブロックは実行されません。どうしてこれなの ?
commitEditingStyle
に[self.tableView setEditing:YES animated:YES];
のレコードを追加していません。それが欠陥なのか?
注:私は(gurantted、と私はそれを投稿didntの理由です)、同じロジックが他のプログラムで動作するため、アレイ・ロジックから私の削除は、間違っているとは思わない
-(void) editpress:(id)sender{
UIBarButtonItem *editButton = (UIBarButtonItem*)self.navigationItem.leftBarButtonItem;
if (!self.tableView.editing) {
[self.tableView setEditing:YES animated:YES];
UIButton *rbut= [UIButton buttonWithType:UIButtonTypeCustom];
[rbut setImage:rightImage forState:UIControlStateNormal];
[rbut addTarget:self action:@selector(editpress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rBarButDone = [[UIBarButtonItem alloc] initWithCustomView:rbut];
self.navigationItem.rightBarButtonItem = rBarButDone;
}
else {
[self.tableView setEditing:NO animated:YES];
UIButton *rbut= [UIButton buttonWithType:UIButtonTypeCustom];
[rbut setImage:rightImage forState:UIControlStateNormal];
[rbut addTarget:self action:@selector(editpress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rBarButEdit = [[UIBarButtonItem alloc] initWithCustomView:rbut];
self.navigationItem.rightBarButtonItem = rBarButEdit;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
// I write the logic to delete the record from the array and the table here.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
[self.tableView endUpdates];
}
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
}
}
オブジェクトはデータソース配列から削除されていないと言いました。 [self.tableView reloadData]を使用しましたか? – karim
はい、私はまた、 'commitEditingStyle'が最初に実行されることを述べました – shajem
配列からオブジェクトを削除するコードを投稿する必要があります。それがあなたの問題です。 –