Cocoa binding NSTableView within NSTableView

夙愿已清 提交于 2019-12-11 03:42:33

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/23575099/cocoa-binding-nstableview-within-nstableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!