简体   繁体   中英

How to go from a managed object context to a table view row?

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?

[self performSegueWithIdentifier:@"viewThreadSegue" sender:managedThread];

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...

managedThread.managedObjectContext

据我了解的Mac OS X的可可研究,您不需要实现数据源协议。您只需将阵列控制器拖到接口生成器中,在身份检查器中将模式设置为实体名称,然后键入之后,您只需要将表视图的每一列绑定到要表示的阵列控制器的键即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM