iPhone Dev: Editing TableView with a DetailView & Core Data


As usual, I wish this was written somewhere while I was trying to figure it out. So now that I have, here I write it for posterity (and others struggling with the same issue!).

Pretend you have a UITableView list of array items or core data values. You want the user to be able to click on one and edit the value- not just add or delete, but actually get in it and change it. One way to do this is to navigate to another view that contains a UITextField, with all of the bells and whistles that the UIKit gives TextFields (edit, keyboard, etc.). Here is how you do it:

In a project that has the Core Data flag set (but you can use just an array if you want- changes for that at the end).

1) Setup a new detailed view controller- in XCode- File/New File/UIViewController SubClass.

2) Move the *.xib (nib) file to the Resources folder. For this example it is named “DetailViewController.”

3) In RootViewController.m, look for the “didSelectRowAtIndexPath” block. Uncomment the “detailviewcontroller” block and add your new nib name “DetailViewController.” It should look like:

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
     NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];

4) We’re going to add a line. This is to pass an object- our retrieved single cell that the user has selected- to a detailed view.

detailViewController.selectedObject = selectedObject;

5) Next we’re going to go to the DetailViewController.h file. Setup two instance variables for our textfield, and the passed row of our fetched results, “selectedObject”. In the @interface block:

NSManagedObject *selectedObject;
        IBOutlet UITextField *fortuneTextField;

also list the properties:

@property (nonatomic, retain) NSManagedObject *selectedObject;
@property (nonatomic, retain) UITextField *fortuneTextField;

6) In the DetailViewController.m file, synthesize and release the instance objects and variables:

@synthesize selectedObject;
@synthesize fortuneTextField;
[fortuneTextField release];
[selectedObject release];

7) Now, we’re going to setup a very simple line in the DetailViewController.m implementation file to pass the selectedObject columnar/attribute value to the UI object. In the “viewDidLoad” block, add:

fortuneTextField.text = [[selectedObject valueForKey:@"FortuneString"] description];

8) Don’t run it yet, we have to setup the textField object and point it to the delegate. In xCode, double-click the DetailViewController.xib file to launch Interface Builder. Create a textfield and position it appropriately (top 1/2 of screen to be visible when keyboard launches). Name it: fortuneTextField (or whatever you named it in your DetailViewController h and m files.)

9) Control-click on the object and drag your mouse to the File Owner. Reverse this- drag the File Owner to the textfield object.

9) Debug and Run!

changing for arrays (not core data)
Instead of creating a NSObject that is a selectedObject, instead create a string/int etc. of the item in your array and pass that to the DetailViewController h/m files.