Editable NSTableView with Bindings won't set values in model

放肆的年华 提交于 2019-12-11 02:28:46

问题


Here is my setup.

In my application delegate, I have a property called currentFoo. The currentFoo property is set to the currently selected Foo instance. The Foo instances each have a property which is a NSMutableArray called results. The objects in results can be of various types, NSNumber, NSString, etc...

I have an NSTableView that is displayed at certain times which allows the user to see the collection of results in the currentFoo. There is also UI to allow adding to and deleting from the results.

The NSTableView has a single column which is bound to an NSArrayController's arrangedObjects controller key. I have written a NSValueTransformer subclass that is used in this binding to convert the various possible types found in the results array to the string representation that I want shown in the table column.

The NSArrayController's content array binding is connected to an NSObjectController's selection controller key, with currentFoo.results as the model key path. Finally the NSObjectController gets its content from the application delegate.

This all works just great, adding, removing and displaying currentFoo.results in the NSTableView works just as I want.

My problem is when I try to edit the content of the table view. I have set my NSValueTransformer subclass to allow reverse transformations and have implemented reverseTransformedValue:, stepping through the code shows me that when I edit the value in the NSTableView row, it is correctly sent to that method of my value transformer, transformed and returned.

The error then occurs when the bindings try to then update the model with the reverse transformed value:

Cocoa Bindings: Error setting value for key path of object 0 (from bound object (null)): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key .

So basically there is this array bound to the table view, and when the user edits the row, I want to transform the string back to the appropriate object. Cocoa bindings gives me this error which leads me to believe I need to set something in the key path of the NSTableColumn binding, but I'm not sure what. I've tried "self" and that didn't work.

Any help is appreciated.


回答1:


The model key cannot be empty—hence the error message you get.

Change the result objects from being primitive objects (strings, boxed numbers, etc.) to model objects (of class FooResult), and set the binding's model key path to the name of the property of those model objects that you will have the table view mutate.

Basically, you can't bind directly to an array of primitive objects. You must bind to some property of an array of model objects.



来源:https://stackoverflow.com/questions/409424/editable-nstableview-with-bindings-wont-set-values-in-model

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