views:

312

answers:

2

I have a basic Core data model like this:

Class

-Class Name (string)

Relationship: HasDetails (to many) -to Details

Details

-Number (int)

-Name (string)

Relationship: IsPartOfClass -to Class

I have two table views, one for "Class" and one for "Details" and have all the bindings set up, however, when I add a new object to Details it shows up for all the Classes, when I want it to show up for only one, the selected one

+3  A: 

There's a lot of information missing here and your description of your entities is confusing. I strongly urge you to take the time to provide far more detail in the future.

It sounds as though you have a Class <---->> Details (one-to-many relationship: a Class has many Details and a Details has one Class). Is that right?

Assuming Class' details relationship is called "details" and Details' class relationship is called "class", and that everything is set up properly in your Managed Object Model (inverse relationships are almost always the right thing to do), you'd want the following:

  1. A "Class" array controller set to Entity mode with entity name set to "Class". The managedObjectContext will need to be bound as well. Your Classes table view would be bound to this.
  2. A "Details" array controller, also set to Entity mode with entity name "Details". You'll bind its "contentSet" to the Class array controller's "selection.details". This will provide your Details array controller only with the selected class's details. The managedObjectContext will need to be bound as well. Your Details table view will be bound to this.

By doing this, calling the Details array controller's -add: -insert: and -delete: (or is it "-remove:"?) methods (such as from a button) will automatically treat the relationship correctly (will add the new "Detail" instance to the selected "Class" instance's details set.

Make sure you bind the add/insert/remove buttons' enabled states to the array controller's "canAdd" and "canRemove" (or is it "canDelete"?) properties so they're disabled if there's no selection in the required places.

Joshua Nozzi
I thought it was relatively clear. And I'm trying what you said right now...
Matt S.
That's because your looking at the datamodel and we are not. When the problem is about relationships a simple text description with no real information about the relationships isn't a lot to go on.
TechZen
Sorry, my previous comment sounded more terse than I intended.
TechZen
I think the comment was spot on, TechZen. Neither mine nor yours were meant as an attack but an honest statement. Matt, when you're asking others for help, it's best not to argue when they say they need clarification. They're doing so to help you and arguing the point doesn't earn you a good reputation, especially on a site like SO.
Joshua Nozzi
Well I've made it as clear as I can without uploading the whole data model
Matt S.
A: 

Matt, let me take a guess what what you're trying to do:

You want a database of classes, each with a name and a number. This wouldn't require a single relationship, and would be very easy to implement!

In the top right pane of the data model view, you would make your class, "Classes" or whatever, and in the next pane to the right, you would press the "+" button to "Add Attribute" for attributes you would title "name" and "number." In the far right column of this view, select "String" for name, and "Integer" for the number.

In interface builder, you will add an array controller to the model view, and then select your array controller, go into the bindings inspector, open the bottom tab for "ManagedObjectContext," and select "File's Owner's" managedObjectContext (Remember to declare this in your View Controller, and deallocate it in the dealloc method definition).

I suspect you will only need one table view, but with multiple columns. Click your table view twice, and in the attributes inspector set the columns to 3. then select each column individually, and in the bindings inspector again, set each column's value to the attribute of the Array Controller you want it to display.

Add a couple buttons that you bind to the array controller's "Add" and "Remove" methods and you should be set.

DanF