将NSTableView绑定到Swift中的字符串数组 [英] Binding an NSTableView to an array of strings in Swift
问题描述
我通过数组控制器绑定通过NSTableView显示字符串数组的内容。
I'm showing the contents of an array of strings with an NSTableView via binding through an Array Controller.
我在属性名称中有NSString数组控制器的检查器和数组控制器的绑定检查器的模型键路径我有我的数组的路径。我有表的唯一列绑定到它的值部分到数组控制器没有指定模型键路径(它只是一个字符串数组)。
I have "NSString" in Class Name in the attributes inspector for the Array Controller and in the Model Key Path of the Array Controller's binding inspector I have the path to my array. And I have the only column of the table bound in its Value section to the Array Controller without Model Key Path specified (it's just an array of strings).
结果,数组的字符串在表中显示得很好。但我无法编辑任何行:
As a result, the array's strings are displayed fine in the table. But I can't edit any of the rows:
2015-06-17 15:48:44.285 ProjectName[9043:123132] An uncaught exception was raised
2015-06-17 15:48:44.285 ProjectName[9043:123132] Error setting value for key path of object five (from bound object <NSTableColumn: 0x618000082d50> identifier: (null)): [<Swift._NSContiguousString 0x608000045d60> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key .
five是我试图编辑的数组中的第五个字符串。正如你可以看到,在path of中有一个空白,因为模型键路径对于列的值是空的。
"five" is the fifth string in the array that I was trying to edit. And as you can see there is a gap in "path of" because the Model Key Path is empty for the column's values.
我以某种方式引用字符串本身在模型键路径中使数组可以通过表格进行编辑?
So do I somehow refer to the string itself in the Model Key Path to make the array editable via the table?
推荐答案
类名应该绝对设置为有效类。更大的问题是,数组控制器并不能很好地与字符串数组一起使用。由于字符串本身正在被编辑(替换),而不是某些属性(如<$ c),因此没有(合理的)使用 -setValue:forKey:
$ c> displayName )。
The Class Name should absolutely be set to a valid class. The bigger problem is that the array controller doesn't really play nicely with arrays of strings. There's no (reasonable) way to use -setValue:forKey:
on a string since the string is itself what's being edited (replaced), not some property (like "displayName
").
我知道这看起来很浪费,但是如果你真的必须使用数组控制器(稍后会更多),你应该创建一个带有string属性的类,将 设置为控制器的类名,并维护该类的数组,而不是纯字符串。说你的字符串数组代表标签。创建类
类的
标签
类 (或 NSString
)。将控制器的类名设置为 Tag
。这种方式,有一个关键路径可以绑定。
I know it seems wasteful, but if you really must use an array controller (more on that in a moment), you should just create a class with a string property and set that as the controller's class name and maintain an array of that class instead of plain strings. Say your array of strings represents tags. Make a Tag
class with a name
property of type String
(or NSString
). Set your controller's class name to Tag
. This way, there's a key path to which to bind.
但是如果你真的没有看到自己需要什么,但字符串数组,你可以只使用标准(和无限更灵活) [NSTableViewDataSource] [1]
协议和按钮触发的好的老式操作(如添加和删除)。这种方式你不打击Cocoa Bindings / KVC / KVO机制,在这种情况下,对于一个非常抽象的控制器,类型(字符串)是太原始的。
But if you really don't see yourself needing anything but an array of strings, you could just use the standard (and infinitely more flexible) [NSTableViewDataSource][1]
protocol and good old-fashioned actions triggered by buttons (like Add and Remove). This way you're not fighting the Cocoa Bindings / KVC / KVO mechanisms for what in this case amounts to too-primitive a type (string) for a very abstract controller.
关于工作量,它几乎是六个一半的其他,但不完全 - 我会与使它成为一个类的名称属性路由,有两个原因:1)它少工作比旋转整个表控制器/数据源,和2)可能你会后来希望你有一个更可扩展的类,而不是一个简单的字符串为一个列表的东西,即使你不这么认为。
As to amount of work, it's almost "six of one half-dozen of the other" but not quite -- I'd go with the "make it a class with a name property" route for two reasons: 1) It's less work than spinning up a whole table controller / data source, and 2) It's likely you'll later wish you had a more extensible class instead of a simple string for a "list of stuff" even if you don't think so now.
这篇关于将NSTableView绑定到Swift中的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!