编辑 CoreData 值

Posted

技术标签:

【中文标题】编辑 CoreData 值【英文标题】:Edit CoreData values 【发布时间】:2013-04-08 02:18:06 【问题描述】:

我一直在尝试将一些数据从 tableview 传递到 editviewcontroller,以便能够对其进行编辑和保存,但我并不走运

我不知道如何将这些值从所选行传递到编辑视图,然后保存新值

这是我的代码:

- (NSManagedObjectContext *)managedObjectContext
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) 
    context = [delegate managedObjectContext];

return context;

-(IBAction)atras:(id)sender
MenuViewController *m=[[MenuViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:m animated:YES completion:nil];


-(IBAction)guardar:(id)sender
NuevaTareaViewController *n=[[NuevaTareaViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:n animated:YES completion:nil];


- (void)viewDidAppear:(BOOL)animated

[super viewDidAppear:animated];

// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Tareas"];
self.tareas = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

[tableView reloadData];


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

// Return the number of sections.
return 1;


 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

// Return the number of rows in the section.
return self.tareas.count;


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];



// Configure the cell...
NSManagedObject *materia = [self.tareas objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [materia valueForKey:@"nombre"]]];
cell.textLabel.textColor=[UIColor whiteColor];
cell.textLabel.font=[UIFont fontWithName:@"Chalkduster" size:(16)];

NSString *detalle=[NSString stringWithFormat:@"%@: Toca para ver los Detalles",[materia valueForKey:@"materia"]];
[cell.detailTextLabel setText:detalle];
cell.detailTextLabel.textColor=[UIColor whiteColor];
cell.detailTextLabel.font=[UIFont fontWithName:@"Chalkduster" size:(12)];

return cell;

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

// Return NO if you do not want the specified item to be editable.
return YES;



 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

NSManagedObjectContext *context = [self managedObjectContext];

if (editingStyle == UITableViewCellEditingStyleDelete) 
    // Delete object from database
    [context deleteObject:[self.tareas objectAtIndex:indexPath.row]];

    NSError *error = nil;
    if (![context save:&error]) 
        NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
        return;
    

    // Remove device from table view
    [self.tareas removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];


- (void)viewDidLoad

[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;


 - (void)didReceiveMemoryWarning

[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

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

NSManagedObject *materia = [self.tareas objectAtIndex:indexPath.row];

NSString *perfil_activo = [materia valueForKey:@"descripcion"];
NSUserDefaults *defs=[NSUserDefaults standardUserDefaults];
//[defs setObject:perfil_activo forKey:@"perfil_activo"];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"texto" message:perfil_activo delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[a show];


谢谢 圣地亚哥

【问题讨论】:

【参考方案1】:

你想获取当前选中的对象。

NSIndex selectedRow = self.tableView.selectedRow;

在你的 tableview 中使用这个索引: NSManagedObject *selectedObject = [self.tableView objectAtIndex:selectedRow]

在您的NuevaTareaViewController 中应该有一个属性: NSManagoedObjectContext Tarea

如果是这样,那么你可以这样做:

-(IBAction)guardar:(id)sender 

NSIndex selectedRow = self.tableView.selectedRow;
NSManagedObject *selectedObject = [self.tableView objectAtIndex:selectedRow];

NuevaTareaViewController *n=[[NuevaTareaViewController alloc] initWithNibName:nil     bundle:nil];

[n setManagedObjectContext:self.context];
[n setTarea:selectedObject];

[self presentViewController:n animated:YES completion:nil];

您可以将托管对象上下文保存在您的 NuevaTareaViewController 中。

话虽如此,请阅读NSFetchedResultsControllerStoryboard Segues,您可能会发现它们很有帮助。

【讨论】:

以上是关于编辑 CoreData 值的主要内容,如果未能解决你的问题,请参考以下文章

如何从 CoreData-Entities 获取一些值到代码中

更新/编辑 coreData 托管对象

在 CoreData 中更新和编辑实体

核心数据:编辑许多属性滞后性能

如何编辑保存在 CoreData 中的文本?

Restkit编辑插入coredata