-->

How can an NSCell detect the model key path it'

2019-08-04 00:24发布

问题:

Imagine the following:

  • You have an NSTableView with multiple columns.
  • Each NSTableColumn is bound to the same NSArrayController (we'll call it myArrayController).
  • This array controller holds many instances of a model class.
  • One column has an NSPopupButtonCell where selectedObject is bound to myArrayController.arrangedObject.somePropertyOfTheModel.
  • The table gets properly populated.

Q: How can an NSCell detect the model key path it's bound to? (somePropertyOfTheModel in this example)

I'm trying to make a cell reusable by not having it assume its represented value is always from somePropertyOfTheModel (could be from somethingElse). Upon a given action, it needs to bind the content of a 2nd controller to somePropertyOfTheModel or somethingElse.

[Edited] A bit more (maybe too much?) explanation: I'm creating a popup-button which displays a few preset values of a property, and a "Custom Value" item which triggers a PopOver window to allows configuration of the property. I want to make it so that I can drop this cell into a table and having it manage the PopOver much like it already manages its own Menu.

What I've tried:

  1. [self representedObject] returns the actual value. Setting it as content to the 2nd controller is all good and well... but whenever the model's property changes, the 2nd controller won't be notified since it's tied to the actual instance of the value... not a binding to the model property.

  2. Querying the cell's binding gives me nothing:

    [[self infoForBinding:@"selectedObject"] objectForKey:NSObservedObjectKey];   // nil returned
    [[self infoForBinding:@"selectedObject"] objectForKey:NSObservedKeyPathKey];  // nil returned
    
  3. Querying the cell's control's (the NSTableView) binding doesn't give me much:

    [(NSTableView*)[self controlView] infoForBinding:@"content"] objectForKey:NSObservedObjectKey];   // returns myArrayController or a poxy to it.
    [(NSTableView*)[self controlView] infoForBinding:@"content"] objectForKey:NSObservedKeyPathKey];  // returns @"arrangedObject"
    // running the same but for @"selectedObject" returns nothing but nils
    

I'd like to query the NSTableColumn itself -- that's where the bindings are defined in IB -- but cell's aren't aware of their existence (unless I've overlooked something obvious). Even passing via NSTableView, no method returns an NSTableColumn for a given cell (and considering prototype cells, I doubt it would help).