【发布时间】:2018-04-11 18:57:09
【问题描述】:
我有一个 NSTableView,我想使用 Interface Builder 方法使其可排序,但我在 Apple 的 Guides and Sample Code 描述如何做到这一点时遇到了麻烦。基于对原始问题的 cmets,我认为该文档已经过时并且指向错误的方向,所以我正在重写我的问题。
在默认的 Cocoa 应用程序模板中添加了以下内容:
class Foo: NSObject {
@objc dynamic var name : String
@objc dynamic var colour : String
override init() {
name = "name"
colour = "colour"
super.init()
}
init(_ name : String, _ colour : String) {
self.name = name
self.colour = colour
super.init()
}
}
class ViewController: NSViewController {
@objc dynamic var fooList : [Foo] = [Foo("Bar", "Red"),
Foo("Baz", "Green"),
Foo("Beelzebub", "Blue")]
}
我在Main.storyboard 的视图控制器中添加了NSArrayController 和NSTableview。 Array Controller 的“Controller Content”绑定到 View Controller 的 Model Key Path self.fooList,Table View 的“Table Content”绑定到 array controller 的默认数据(Controller Key of arrangedObjects),每个表格视图单元格的“值”绑定到表格单元格视图,“模型键路径”分别为objectValue.name 和objectValue.colour。到目前为止,一切正常,我得到了填充表格视图的数据。
根据 cmets 中的建议,为了使列表可排序,我正在尝试添加“排序描述符”绑定。我已将表视图的“排序描述符”绑定到数组控制器(默认选项),并将数组控制器的排序描述符绑定到视图控制器(添加了self.fooList 的“模型键路径”)。
此时,如果我尝试运行应用程序,我会收到异常并回溯:
2018-04-15 17:45:13.354272-0400 ACTest[48227:16210514] -[ACTest.Foo key]: unrecognized selector sent to instance 0x61000026c900
2018-04-15 17:45:13.356022-0400 ACTest[48227:16210514] [General] An uncaught exception was raised
2018-04-15 17:45:13.356042-0400 ACTest[48227:16210514] [General] -[ACTest.Foo key]: unrecognized selector sent to instance 0x61000026c900
2018-04-15 17:45:13.356105-0400 ACTest[48227:16210514] [General] (
0 CoreFoundation 0x00007fff57caffcb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff7e94dc76 objc_exception_throw + 48
2 CoreFoundation 0x00007fff57d48a24 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff57c26610 ___forwarding___ + 1456
4 CoreFoundation 0x00007fff57c25fd8 _CF_forwarding_prep_0 + 120
5 AppKit 0x00007fff5540eb48 -[NSArrayController automaticRearrangementKeyPaths] + 216
6 AppKit 0x00007fff5540e95d -[NSArrayController _updateAutomaticRearrangementKeysPaths] + 30
7 AppKit 0x00007fff555706ef -[NSArrayController _didChangeArrangementCriteriaWithOperationsMask:useBasis:] + 53
8 AppKit 0x00007fff5557055e -[NSArrayController setSortDescriptors:] + 219
9 AppKit 0x00007fff5570a6c4 -[NSControllerConfigurationBinder _updateSortDescriptors:] + 73
10 AppKit 0x00007fff5570aa7b -[NSControllerConfigurationBinder _observeValueForKeyPath:ofObject:context:] + 279
11 AppKit 0x00007fff55260cff -[NSObject(NSKeyValueBindingCreation) bind:toObject:withKeyPath:options:] + 782
12 AppKit 0x00007fff551dcbbd -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1430
13 AppKit 0x00007fff552d813a -[NSNib _instantiateNibWithExternalNameTable:options:] + 679
14 AppKit 0x00007fff552d7d96 -[NSNib _instantiateWithOwner:options:topLevelObjects:] + 136
15 AppKit 0x00007fff55a2e180 -[NSStoryboard instantiateControllerWithIdentifier:] + 236
16 AppKit 0x00007fff551d0ecf NSApplicationMain + 729
17 ACTest 0x000000010000497d main + 13
18 libdyld.dylib 0x00007fff7f53d115 start + 1
19 ??? 0x0000000000000003 0x0 + 3
)
2018-04-15 17:45:13.384522-0400 ACTest[48227:16210514] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ACTest.Foo key]: unrecognized selector sent to instance 0x61000026c900'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff57caffcb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff7e94dc76 objc_exception_throw + 48
2 CoreFoundation 0x00007fff57d48a24 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff57c26610 ___forwarding___ + 1456
4 CoreFoundation 0x00007fff57c25fd8 _CF_forwarding_prep_0 + 120
5 AppKit 0x00007fff5540eb48 -[NSArrayController automaticRearrangementKeyPaths] + 216
6 AppKit 0x00007fff5540e95d -[NSArrayController _updateAutomaticRearrangementKeysPaths] + 30
7 AppKit 0x00007fff555706ef -[NSArrayController _didChangeArrangementCriteriaWithOperationsMask:useBasis:] + 53
8 AppKit 0x00007fff5557055e -[NSArrayController setSortDescriptors:] + 219
9 AppKit 0x00007fff5570a6c4 -[NSControllerConfigurationBinder _updateSortDescriptors:] + 73
10 AppKit 0x00007fff5570aa7b -[NSControllerConfigurationBinder _observeValueForKeyPath:ofObject:context:] + 279
11 AppKit 0x00007fff55260cff -[NSObject(NSKeyValueBindingCreation) bind:toObject:withKeyPath:options:] + 782
12 AppKit 0x00007fff551dcbbd -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 1430
13 AppKit 0x00007fff552d813a -[NSNib _instantiateNibWithExternalNameTable:options:] + 679
14 AppKit 0x00007fff552d7d96 -[NSNib _instantiateWithOwner:options:topLevelObjects:] + 136
15 AppKit 0x00007fff55a2e180 -[NSStoryboard instantiateControllerWithIdentifier:] + 236
16 AppKit 0x00007fff551d0ecf NSApplicationMain + 729
17 ACTest 0x000000010000497d main + 13
18 libdyld.dylib 0x00007fff7f53d115 start + 1
19 ??? 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我不清楚从这里到哪里去。任何帮助表示赞赏。
【问题讨论】:
-
将表格视图的排序描述符绑定到数组控制器的
sortDescriptors。还要对 ViewController 中的数组进行排序吗? -
将 NSTableView 的排序描述符绑定到数组控制器似乎还不够,并在视图加载时导致
unrecognized selector sent to instance错误。从您的消息中不清楚如何将其定向到阵列控制器的sortDescriptors。模型键路径不这样做(导致另一个错误)。 -
不……我不需要从 ViewController 中对数组进行排序。
-
哪个选择器不能被哪个对象/类识别?如果您不想从 ViewController 对数组进行排序,则不需要任何代码。绑定将数组控制器的
sortDescriptors设置为表格视图的sortDescriptors,数组控制器将自动对数据进行排序。列的排序键用于创建NSSortDescriptor,name是正确的。 -
2018-04-15 17:45:13.354272-0400 ACTest[48227:16210514] -[ACTest.Foo key]: unrecognized selector sent to instance 0x61000026c900数组控制器连接到视图控制器类中的@objc dynamic var fooList : [Foo] = [...]。
标签: swift4 nstableview xcode9