Reputation: 111
Now my problem is not one but two, which are as follows:
App Requirement: Input details of employees and click on Save button. the name of the employee should get added in the table view. Click on a single row, details should be displayed in the detail view.
Problem 1: after clicking save button for the first time, it's displayed in the table view but from the next clicking, it's not displayed until and unless the app is relaunched. My code for this is as follows:
(void)saveDetails{
EmployeeDetailsAppDelegate *appDelegate = (EmployeeDetailsAppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newDetails;
newDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:context];
[newDetails setValue:empID.text forKey:@"EmployeeID"];
[newDetails setValue:empName.text forKey:@"EmployeeName"];
[newDetails setValue:empDepartment.text forKey:@"EmployeeDepartment"];
empID.text = @"";
empName.text = @"";
empDepartment.text = @"";
[[appDelegate rootViewController]addNewObject:empName.text];
NSError *error;
[context save:&error];
[self dismissModalViewControllerAnimated:YES];
}
Problem 2: This is basically not a problem but a query. On clicking a row in the table view, the employee department is displayed in the detail view. Now I want to display the employee id as well. How can I do that? My code for employee department display is:
(void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.array count]){
dictionary = [self.array objectAtIndex:indexPath.row] ;
if([dictionary objectForKey:@"EmployeeDepartment"]){
[dictionary objectForKey:@"EmployeeDepartment"];
NSString *empDep = [dictionary objectForKey:@"EmployeeDepartment"];
NSLog(@"Dep is %@",empDep);
}
}
detailViewController.detailItem = [NSString stringWithFormat:@"Employee Department: %@",[dictionary objectForKey:@"EmployeeDepartment"]];
self.navigationItem.leftBarButtonItem.enabled = YES;
}
PS: This is for iPad with Core data used as storage.
Upvotes: 0
Views: 47
Reputation: 90117
when you use core-data with a UITableView you should definitely use a NSFetchedResultsController and its delegate methods.
If I understood your first problem correctly this should solve it. And it will solve much much more problems you will have later with your data array that tries to mirror core-data objects. I say try because it obviously doesn't work.
In short, the NSFetchedResultsController "monitors" the core-data objects you want to display in your tableview, and if an object is changed it will tell your delegate, which will take the appropriate action (like insert or delete a row). The documentation has complete sample code for the delegate methods you have to implement.
Regarding the second question: Change your DetailViewController so it can handle the NSManagedObject. A detailView where you can't edit the object is usually useless, so you would need to do that sooner or later anyway.
Upvotes: 1