编辑特定的CoreData条目

时间:2014-04-07 21:00:13

标签: ios xcode uitableview core-data

再一次,由于Core Data对我来说很新,我需要帮助。基本上,我需要从 tableview 之外的视图编辑特定的Core Data对象,保存它,然后重新加载tableview。我可以管理重新加载tableview,但我无法弄清楚如何为该indexPath条目找到特定的Core Data对象。我没有任何代码,因为我找不到任何符合我目的的东西。

细节(编辑)

-(IBAction)cancel:(id)sender{


    NSLog(@"Sent!");
 //   [self dismissViewControllerAnimated:YES completion:nil];
//    [self.view.superview removeFromSuperview];
    OweDetails *details = info.details;


    [info setValue:oField.text forKey:@"name"];

    info.name = oField.text;

    [UIView animateWithDuration:0.5
                          delay:1.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         self.master = [[MasterViewController alloc]init];
                         [self dismissSemiModalViewController:self];
                         [self.master.tableView reloadData];

                     }
                     completion:^(BOOL finished){
                         NSLog(@"Done!");
                     }];




}

Master(TableView)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"I tapped myself!");

        OweInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];
    self.tdModal = [[TDSemiModalViewController alloc]init];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FooterViewController *fvc = [[FooterViewController alloc]init];
    OweDetails *details = info.details;
    [self.tdModal setDetailItem:info.name];
    [self.tdModal setMoneyItem:details.money];
//    [self.view addSubview:test.view];

    self.tdModal.managedObjectContext = self.managedObjectContext;



    self.tdModal.managedObjectContext = self.managedObjectContext;
    OweInfo *info2 = (OweInfo *)[nameArray objectAtIndex:indexPath.row];
    self.tdModal.info = info2;



                      [self presentSemiModalViewController:self.tdModal];









}

2 个答案:

答案 0 :(得分:0)

AppDelegate* appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext* context = [appDelegate managedObjectContext];   
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"YOUR_ENTITY_NAME" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SOME_PREDICATE_USED_TO_UNIQUELY_IDENTIFY_YOUR_INDEXPATH ==%@", @"VALUE"];
[fetchRequest setPredicate:predicate];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setPredicate:predicate];

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

if (error)
{
    NSLog(@"Error fetching data. %@", error);
    return nil;
}

if (array.count != 1)
{
    NSLog(@"Invalid number (%d) of objects found", array.count);
    return nil;
}

return [array objectAtIndex:0];

编辑:这是为了回应您询问更多详细信息。 上面的代码可以是它自己的函数。我将为您提供一个如何使用它的简单示例。

- 在您拥有该表并显示数据的VC中

1:假设您的实体名为MyEntity,并且它有一个名为myEntityUniqueId的整数唯一标识符字段。

2:在你的cellForRowAtIndexPath中,你会写:

cell.tag = myCurrentEntity.myEntityUniqueId

- 现在让我们转到您想要检索被点击的单元格所代表的MyEntity实例的其他VC。

1:写下函数:

-(void) findMyEntityForId:(int) tappedUniqueId
{
      AppDelegate* appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext* context = [appDelegate managedObjectContext];   
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"MyEntity" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"myEntityUniqueId ==%d", tappedUniqueId];
[fetchRequest setPredicate:predicate];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setPredicate:predicate];

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

if (error)
{
    NSLog(@"Error fetching data. %@", error);
    return;
}

if (array.count != 1)
{
    NSLog(@"Invalid number (%d) of objects found", array.count);
    return;
}

  MyEntity* foundResult = [array objectAtIndex:0]; // <-- This is the instance you are looking for.
}

2:现在用正确的参数调用上面的函数。

我没有测试过代码,但应该没问题。

答案 1 :(得分:0)

如果您正在使用NSFetchedResultsController,请询问objectAtIndexPath: