简体   繁体   English

如何从托管对象上下文转到表视图行?

[英]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 . 我有一个UITableView ,使用CoreData SQLLite数据源和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. 不过,我想更进一步,并执行我已设置的segue,然后单击该项目的UITableViewCell时发生。 So I'm going to initiate that segue in code, using the performSegueWithIdentifier:sender method. 因此,我将使用performSegueWithIdentifier:sender方法在代码中启动该segue。 The sender has to be the UITableViewCell , though. 但是,发送者必须是UITableViewCell

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. 我需要使用该数据为新创建的项目定位UITableViewCell

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. 现在,您可以在performSeque中将托管对象传递给视图控制器。 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的可可研究,您不需要实现数据源协议。您只需将阵列控制器拖到接口生成器中,在身份检查器中将模式设置为实体名称,然后键入之后,您只需要将表视图的每一列绑定到要表示的阵列控制器的键即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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