带有绑定的可编辑NSTableView不会在模型中设置值 [英] Editable NSTableView with Bindings won't set values in model

查看:97
本文介绍了带有绑定的可编辑NSTableView不会在模型中设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的设置。



在我的应用程序委托中,我有一个名为currentFoo的属性。 currentFoo属性设置为当前选择的Foo实例。 Foo实例每个都有一个属性,它是一个NSMutableArray调用的结果。结果中的对象可以是各种类型,NSNumber,NSString等...



我有一个NSTableView在某些时候显示,允许用户看到在currentFoo中收集结果。还有一个UI允许添加和从结果中删除。



NSTableView有一个列绑定到NSArrayController的arrangeObjects控制器键。我写了一个NSValueTransformer子类,用于这个绑定,以将在results数组中找到的各种可能的类型转换为我想显示在表列中的字符串表示。



NSArrayController的内容数组绑定连接到NSObjectController的选择控制器键,其中currentFoo.results作为模型键路径。最后NSObjectController从应用程序委托获得它的内容。



这一切都很好,添加,删除和显示currentFoo.results在NSTableView工作就像我想要的。 / p>

我的问题是当我尝试编辑表视图的内容。我设置了我的NSValueTransformer子类允许反向转换,并已实现reverseTransformedValue:,通过代码显示我,当我编辑NSTableView行中的值,它被正确发送到我的值变换器的方法,转换和返回。



当绑定尝试使用反向转换值更新模型时,会发生错误:


可可绑定:对于对象0的关键路径错误设置值
(从绑定
对象(null)):[setValue:forUndefinedKey:]:
此类不是键值
为键的编码。


所以基本上有这个数组绑定到表视图,用户编辑行,我想将字符串转换回适当的对象。 Cocoa绑定给我这个错误,这让我相信我需要设置一些在NSTableColumn绑定的关键路径,但我不知道什么。



任何帮助。



将结果对象从原始对象(字符串,盒子)中更改为非原始对象数字等)到模型对象(类FooResult),并将绑定的模型键路径设置为这些模型对象的属性的名称,您将使表视图mutate。



基本上,你不能直接绑定到一个基本对象数组。您必须绑定到模型对象数组的某个属性。


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.

解决方案

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.

这篇关于带有绑定的可编辑NSTableView不会在模型中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆