jsd
jsd

Reputation: 7703

Cocoa binding NSTableView within NSTableView

I'm trying to display a NSTableView inside a NSTableView. This is for an iTunes-like albums/tracks view. So there's a list of 10 albums, each album has some tracks. I want to display the albums in the outer table view and the tracks for each album in the inner.

The first NSTableView is bound to an NSArrayController. Each object in the array has its own "tracks" NSArrayController, but I can't figure out how to tell the 'tracks' NSTableView that its content comes from a property of the 'album' NSTableView.

Upvotes: 2

Views: 851

Answers (2)

George Brown
George Brown

Reputation: 1144

If I understand you right, the source content of the nested array controller comes from objectValue of the owner table cell. So you can't put the array controller content source to be the objectValue of the table cell. I'm doing similar in that I want to filter the array content based on the object value

What I'm doing, which seems to be working, is to make a seperate nib file for your nested table cell view, with it's own nstablecellview subclass. Include the array controller in the nib and create an outlet to it in your cell view subclass.

Register it with the table view in the viewDidLoad method of the tables view controller:

NSNib *cellView = [[NSNib alloc] initWithNibNamed:@"MyTableCellView" bundle:nil];
[myTableView registerNib:cellView forIdentifier:@"myTableCellView"];

Then, in the awakeFromNib method of your cell view subclass, manually make your bindings that require the object value:

[self.arrayController bind:@"contentSet"
                  toObject:self
               withKeyPath:@"objectValue.tracks"
                   options:nil];

Voila.

Note that when using this technique, the files Owner of the nib file is not your nstablecellview subclass, it is the view controller of the table view.

Upvotes: 2

Elise van Looij
Elise van Looij

Reputation: 4232

The problem lies in not understanding the MVC-pattern (Model-View-Controller). Content for a view never comes from another view, it comes from model objects via a controller. The content of each of your tableviews always comes from an NSObjectController, or a subclass like NSArrayController. Basically, there are two solution:

  • bind the 'tracks' tableview to the selection of the 'album' array controller
  • create a 'tracks' array controller and bind it to the selection of the 'album' array controller. Bind the 'tracks' tableview to the 'tracks' array controller

A third solution is to use an NSTreeController with an NSOutlineView but outline views and tree controllers are notoriously hard to work with.

Upvotes: -1

Related Questions