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;
}];
}