views:

194

answers:

2

I really hope someone can help on this because I'm learning cocoa and have hit a road block.

I am trying to model a simple poker tournament. For now, my entities are simply a Tournament (with a number) and a Player (with a Name). A Tournament has an array of Players.

I can bind two independent table views to display the tournaments and the players just fine. But I want the players table view to just show the players that belong to the selected tournament from the first table view.

Each has it's own array controller. I have tried a variety of different bindings for the second (players) table but to no avail. Has anyone accomplished this? If so maybe you could spell it out for me, as I there are few examples online.

  • Update I can now ALMOST get where I need to, mostly through rial and error and hours of googling. I have bound the player AC's content to the tournament AC, with controller key 'selected objects' and Model Key Path 'players', which is the name of the array in my Tournament entity.

I have the bound the column in the players table view to this second Player AC, controller key arranged objects. But what to put in the Model Key Path? I know it is working because if I stick @count in there I get the correct number of players for the selected tournament. But 'name' and 'player.name' are no good. Is there any kind of 'item.name' or 'players.item.name' I can try?

Sooo close, thanks for the help so far: alt text

+3  A: 

I think this tutorial will help you. They also create a master/detail view.

In short: Bind the contentArray of your player's array controller to the tournament's array controller, set ControllerKey to selection and the remaining properties accordingly to your model.

Felix Kling
Ok, that tutorial looks really useful - thanks for the link, I'm working through it now.My current project uses core data, with an entity model and a managed object context. This has worked fine for regular bindings, and has the benefit that I can programmatically create objects when it is launched (and insert them into the same context). This behaviour is essential.The tutorial uses dictionary objects rather than regular properties, and of course has no MOC. This will require a drastic re-write of my existing command line app. Should I be start over?I'm willing to start over, but
Ben Packard
Just look around the site there is also a tutorial for Core Data (with binding) http://cocoadevcentral.com/articles/000085.php In the end, you have to decide what fits your needs better.
Felix Kling
Thanks. I just wanted to check I wasn't on the wrong track entirely, it sounds like either approach is valid. I have got the hang of core data and bindings, but just not this combination of core data and master-detail table views. I'll look around on cocoadev.
Ben Packard
A: 

I found the answer here:

http://stackoverflow.com/questions/1697872/implementing-parent-child-drill-down-in-cocoa-with-core-data-bindings-that-span.

  • The child controller needs to know about the managedObjectContext through its own binding.
  • The child controller must not be in Entity Mode, but rather operate as a NSMutableDictionary class.
  • And, finally, the child controller does not prepare its data. It retrieves it from the parent, through the Content Set binding. Use the controller key selection, and the model key path that connects to the children.

I'm surprised this is not a more commonly used practice, and hope the next person reading this doesn't spend so long finding the answer!

Ben Packard
If the objects in the array are managed objects, then the array controller should be set accordingly. It definitely should not be set to NSMutableDictionary if they aren't NSMutableDictionaries. Don't lie to Cocoa—nothing but problems can result. Felix's answer is the correct one.
Peter Hosey
No, it isn't (or wasn't for me). And I don't if you realise that your answer comes across pretty arrogant, which doesn't help either me or anyone else reading this. I spent three or four 16-hour days chasing this down - the tutorial posted doesn't help in my particular set of circumstances, where I need to insert object created from a separate process into the MOC. The summary I posted above is a verbatim overview from the link - did you bother to read the whole post?
Ben Packard