我一直在尝试将一些数据从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];
}
由于 圣地亚哥
答案 0 :(得分:0)
您想要获取当前选定的对象。
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
。
话虽如此,请阅读NSFetchedResultsController
和Storyboard Segues
,您可能会发现它们非常有用。