ユーザーがデータベースにエントリを追加できるようにする簡単なアプリを作成しています。データは UITableView に表示されます。私が理解できないのは、テーブルビューのスワイプ削除機能を使用して、データベースから 1 つのレコードだけを削除する方法です。私は、コードがこのメソッドに入ることを知っています:
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
}
しかし、スワイプされたセルにデータを入力するデータベースのレコードを取得する方法がわかりません。
ユーザーがナビゲーションバーのボタンをクリックすると、すべてのセルを削除するメソッドがあります。
-(void)deleteAll {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Parameters" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *items = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *managedObject in items) {
[context deleteObject:managedObject];
}
if (![context save:&error]) {
}
[self.tableView reloadData];
}
しかし、一度に 1 つのレコードを削除するようにこのコードをカスタマイズする方法はわかりません。私を始めるための助けをいただければ幸いです。
私もこの方法を持っています...これはレコードを完全に削除すると思いますが、そうではありません...
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.arr removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
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];
}