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

时间:2021-04-20 19:49:19

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. So I'm going to initiate that segue in code, using the performSegueWithIdentifier:sender method. The sender has to be the UITableViewCell, though.

我想更进一步,然后执行我已设置的segue,然后在单击任何项​​目的UITableViewCell时发生。所以我将使用performSegueWithIdentifier:sender方法在代码中启动segue。但是,发件人必须是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];

}

2 个解决方案

#1


1  

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.

现在,在您的performSeque中,您可以将托管对象传递给视图控制器。无需使用与您正在执行的操作不自然的索引路径污染所有代码。

NOTE: you can ask the object for its managed object context...

注意:您可以询问对象的托管对象上下文...

managedThread.managedObjectContext

#2


0  

From what I know studying cocoa for Mac OS X, you don't need to implement the data source protocol.You can just drag an array controller to interface builder, in the identity inspector set the mode to entity name, and type the name of the data source entity that you have created.After this you just have to bind each column of the table view to the array controller's key that you want to represent.

据我所知,为Mac OS X学习cocoa,您不需要实现数据源协议。您只需将数组控制器拖到界面构建器,在身份检查器中将模式设置为实体名称,然后键入名称您创建的数据源实体。此后,您只需将表视图的每一列绑定到要表示的数组控制器的键。

#1


1  

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.

现在,在您的performSeque中,您可以将托管对象传递给视图控制器。无需使用与您正在执行的操作不自然的索引路径污染所有代码。

NOTE: you can ask the object for its managed object context...

注意:您可以询问对象的托管对象上下文...

managedThread.managedObjectContext

#2


0  

From what I know studying cocoa for Mac OS X, you don't need to implement the data source protocol.You can just drag an array controller to interface builder, in the identity inspector set the mode to entity name, and type the name of the data source entity that you have created.After this you just have to bind each column of the table view to the array controller's key that you want to represent.

据我所知,为Mac OS X学习cocoa,您不需要实现数据源协议。您只需将数组控制器拖到界面构建器,在身份检查器中将模式设置为实体名称,然后键入名称您创建的数据源实体。此后,您只需将表视图的每一列绑定到要表示的数组控制器的键。