我一直在尝试将一些数据从 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];
}
谢谢圣地亚哥