通过 Interface Builder 使 NSTableView 列可排序

Making NSTableView columns sortable via Interface Builder

我有一个 NSTableView,我想使用 Interface Builder 方法对其进行排序,但我在使用 Apple 的 Guides and Sample Code 描述时遇到了麻烦。根据对原始问题的评论,我认为该文档已经过时并且指向了错误的方向,因此我正在重写我的问题。

默认 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 中的视图控制器中添加了 NSArrayControllerNSTableview。 Array Controller 的 "Controller Content" 绑定到具有 Model Key Path self.fooList 的 view controller,Table View 的 "Table Content" 绑定到具有默认数据的 array controller(Controller Key arrangedObjects),并且每个 Table View Cell 的 "Value" 绑定到具有 objectValue.name 和 [=18 的 "Model Key Path" 的 Table Cell View =] 分别。到目前为止,一切正常,我得到了填充 Table 视图的数据。

根据评论中的建议,为了使列表可排序,我正在尝试添加 "Sort Descriptors" 绑定。我已将 Table 视图的 "Sort Descriptors" 绑定到数组控制器(默认选项),并将数组控制器的排序描述符绑定到视图控制器(添加 "Model Key Path" of 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)  

我不清楚从这里到哪里去。感谢任何帮助。

这是 Cocoa 绑定的一个经典问题:当它起作用时,就像变魔术一样,但当它不起作用时,您不知道如何修复它。我假设您已经填充了 table 内容,所以我不会谈及它。以下是处理排序的方法:

对于 table 视图:

将 table 视图的排序描述符绑定到数组控制器的 sortDescriptors:

对于 table 列:

为名称列设置排序键=名称,为颜色列设置排序键=颜色。不需要额外的绑定。大功告成!