I have a UITableView, using a CoreData SQLLite data source and NSManagedObjectContext.
Forgive me if I do not get this terminology correct, because I’ve only been at this for a few days now, so I’m just learning.
I just finished setting up a modal view to let me add new items to my data source. So, my logic right now is basically: add item to data source, refresh table view, I can now see my new item listed.
I want to take it a step farther, though, and perform a segue that I have setup that then occurs when the UITableViewCell for that any item is clicked. So I’m going to initiate that segue in code, using the performSegueWithIdentifier:sender method. The sender has to be the UITableViewCell, though.
So, given the things I have, I have the data that I just added to my data source, but do not know the index of the new item in the data source. I need to use that data to locate the UITableViewCell for the newly created item.
Any ideas on how to do this? I’ve been looking around and I have not seen any example code that looks like what I’m expecting to see.
Below is my code for adding the new item, from my modal view, to the data source:
#pragma mark - ComposeThreadViewControllerDelegate Methods
- (void)composeThreadViewController:(ComposeThreadViewController *)controller didFinishComposing:(Thread *)thread {
// get the context
NSManagedObjectContext *context = [(id)[[UIApplication sharedApplication] delegate] managedObjectContext];
// add this new thread to our local cache
NSManagedObject *managedThread = [NSEntityDescription insertNewObjectForEntityForName:@"Thread" inManagedObjectContext:context];
[managedThread setValue:thread.id forKey:@"id"];
[managedThread setValue:thread.title forKey:@"title"];
[managedThread setValue:thread.author forKey:@"author"];
[managedThread setValue:thread.text forKey:@"text"];
[managedThread setValue:thread.location forKey:@"location"];
//save the new thread
[context save:nil];
// begin refreshing the list of threads
[self.tableView reloadData];
// dismiss the modal view
[self dismissModalViewControllerAnimated:YES];
// bring up the view item view
[self performSegueWithIdentifier:@"viewThreadSegue" sender:self];
}
Why do you need the index path of the object? That is an implementation detail of the table view, which is managing the presentation of your objects.
If you need the object, why not just pass the managed object as the sender?
Now, in your performSeque, you can pass the managed object to the view controller. No need to pollute all of your code with index paths that are unnatural to what you are doing.
NOTE: you can ask the object for its managed object context…