带有绑定的可编辑 NSTableView 不会在模型中设置值

发布于 2024-07-10 22:02:38 字数 1106 浏览 4 评论 0原文

这是我的设置。

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

我有一个在特定时间显示的 NSTableView,它允许用户查看 currentFoo 中的结果集合。 还有 UI 允许添加和删除结果。

NSTableView 有一个列绑定到 NSArrayController 的arrangedObjects 控制器键。 我编写了一个 NSValueTransformer 子类,在此绑定中使用它来将结果数组中找到的各种可能类型转换为我想要在表列中显示的字符串表示形式。

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

这一切都很好,在 NSTableView 中添加、删除和显示 currentFoo.results 就像我想要的那样。

我的问题是当我尝试编辑表视图的内容时。 我已将 NSValueTransformer 子类设置为允许反向转换,并实现了reverseTransformedValue:,单步执行代码显示,当我编辑 NSTableView 行中的值时,它会正确发送到我的值转换器的该方法,进行转换并返回。

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

Cocoa Bindings:设置值错误 对于对象 0 的关键路径(从绑定 对象(空)):[ setValue:forUndefinedKey:]: 这个类不是键值 密钥的编码兼容。

所以基本上这个数组绑定到表视图,当用户编辑行时,我想将字符串转换回适当的对象。 Cocoa 绑定给了我这个错误,这让我相信我需要在 NSTableColumn 绑定的关键路径中设置一些东西,但我不确定是什么。 我尝试过“自我”,但没有成功。

任何帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2024-07-17 22:02:38

模型键不能为空,因此您会收到错误消息。

将结果对象从原始对象(字符串、盒装数字等)更改为模型对象(FooResult 类),并将绑定的模型键路径设置为您将拥有表视图的那些模型对象的属性名称变异。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文