0

ユーザーがデータベースにエントリを追加できるようにする簡単なアプリを作成しています。データは 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];
}
4

2 に答える 2

0

MCSwipeTableCellを使用して機能を削除するために同じスワイプを行いました

アニメーション付きのテーブルから特定の行を削除するには、次のようにします。

 //I am passing 0,0 so you gotta pass the row that was deleted.
 NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0]

 [self.yourTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

コア データから削除するには、次のようにします。

YourCustomModel *modelObj;

NSManagedObjectContext *context= yourmanagedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"YourCustomModel" inManagedObjectContext:context]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"yourField == %@", passTheFieldValueOfTheRowDeleted];
[request setPredicate:predicate];

NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];

if ([results count]>0)
{
    modelObj = (Customers *)[results objectAtIndex:0];
}

[context deleteObject:modelObj];

if (![context save:&error])
{
    NSLog(@"Sorry, couldn't delete values %@", [error localizedDescription]);
}
于 2013-04-12T04:36:17.817 に答える
0

もうすぐそこです..フェッチリクエストに述語を追加するだけです。

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(somePropertyInParameters = %d)",value]; 
   [fetchRequest setPredicate:predicate];`

また、このメソッドでスワイプされたセル (および Parameter オブジェクト) のインデックス パスを取得できます。

  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath` 
于 2013-04-12T04:39:49.923 に答える