1

Core Data の編集/保存に苦労しており、これについて助けが必要です。私は NSFetchedResultsController を使用しており、Core Data に first_name、last_name、email_id などの属性を持つ Golfer という名前のエンティティがあります。ですから、ゴルファーをデータベースに追加および削除する方法を知っています。

私は ViewManager と呼ばれる 1 つのビュー コントローラー (すべてのクラスの基本ビューのようなもの) に取り組んでおり、その中に 2 ~ 3 個のカスタム UIView があります。必要なときはいつでもアニメーション化します。

ゴルファーをテーブルビューに追加し、didSelectRow テーブルビュー メソッドで、同じ ViewManager コントローラー内に編集ビューを表示し、次のコードを使用して編集ビューのテキスト フィールドを更新しようとしましたが、テーブルビューのランダム インデックスで更新されていません。私のために働いています。どんな助けでも大歓迎です。

- (IBAction)saveEditGolfersView:(id)sender
{
    AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext * context = [applicationDelegate managedObjectContext];

    // Retrieve the entity from the local store -- much like a table in a database
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Golfer" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity]; 

    // Set the sorting -- mandatory, even if you're fetching a single record/object
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"first_name" ascending:YES];

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release]; sortDescriptors = nil;
    [sortDescriptor1 release]; sortDescriptor1 = nil;

    NSError * error;

    NSArray * objects = [context executeFetchRequest:request error:&error];

    for(int i = 0; i<[objects count]; i++)
    {
        Golfer * golfguy = [objects objectAtIndex:i];

        golfguy.first_name = mEditFirstName.text;
        golfguy.middle_name = mEditMiddleName.text;
        golfguy.last_name = mEditLastName.text;
        golfguy.email_id = mEditEmailField.text;
        golfguy.contactNumber = mEditContactNum.text;
        golfguy.picture = mEditPictureView.image;

        NSLog(@"name-%@", golfguy.first_name);
    }

    [request release]; request = nil;

     error = nil;
    [context save:&error];

    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut

                     animations:^ {

                         mEditGolfersView.frame = CGRectMake(-480, mEditGolfersView.frame.origin.y, mEditGolfersView.frame.size.width, mEditGolfersView.frame.size.height);

                     }

                     completion:^(BOOL finished) {
                         mEditGolfersView.hidden = YES;

                     }];
}
4

1 に答える 1

1

このコードを正しく読んだ場合、を呼び出すと、-(IBAction)saveEditGolfersView:(id)senderすべてのゴルファーに正確なプロパティが設定されますが、これはあなたが望むものではないと思います。

問題が何であるかはよくわかりませんが、正しいゴルファーを変更するには、NSPredicate一緒に行く必要があると思います。NSFetchRequest

何かが足りなかったかもしれませんが、このコードは「ねえ、データベースにすべてのゴルファーをロードし、名前で並べ替えてから、すべてのプロパティをこれとまったく同じテキストフィールドに設定します。ページ"。悪いニュースのように聞こえます...

1人のゴルファーだけを編集するには、編集しているゴルファーを必ずどこかのプロパティに保存してください。managedObjectContextをapplicationDelegateに保存しておくので、それは存続し、コアデータオブジェクトを存続させます。これにより、保存ビューで実行している高価なフェッチを回避できます。ただし、ゴルファーオブジェクトへの参照を保持したくない場合は、それぞれNSManagedObjectobjectIdコアデータで使用される識別子であるがあります。objectId次のように、フェッチ述語でを使用できます。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"objectId == %@", self.editingGolferObjectId];
[request setPredicate:predicate];

私はあなたの場合、オブジェクトではなく、オブジェクトへの参照を維持することを選択しますobjectId

于 2012-04-20T00:22:35.003 に答える