通过 Interface Builder 使 NSTableView 列可排序

mpo*_*ett 4 nstableview swift4 xcode9

我有一个 NSTableView,我想使用 Interface Builder 方法对其进行排序,但我在 Apple 的指南和示例代码描述中遇到了问题。根据对原始问题的评论,我认为这个文档已经过时了,并且指向了错误的方向,所以我正在重写我的问题。

鉴于对默认 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")]

}
Run Code Online (Sandbox Code Playgroud)

我已经添加的NSArrayController,并NSTableview在该视图控制器Main.storyboard。Array Controller 的“Controller Content”绑定到带有 Model Key Path 的视图控制器self.fooList,Table View 的“Table Content”绑定到带有默认数据(Controller Key of arrangedObjects)的数组控制器,以及每个 Table View Cell 的“Value”被绑定到表格单元格以查看一个“模型关键路径”objectValue.nameobjectValue.colour分别。到目前为止,一切正常,我得到了填充表视图的数据。

根据评论中的建议,为了使列表可排序,我正在尝试添加“排序描述符”绑定。我已将表视图的“排序描述符”绑定到数组控制器(默认选项),并将数组控制器的排序描述符绑定到视图控制器(添加了 的“模型键路径” 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)  
Run Code Online (Sandbox Code Playgroud)

我不清楚从这里去哪里。任何帮助表示赞赏。

Cod*_*ent 6

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

对于表格视图:

将 table view 的 Sort Descriptors 绑定到 Array Controller 的sortDescriptors:

绑定表的排序描述符

对于表列:

为 Name 列设置Sort Key = name,为 Color 列设置Sort Key = color。不需要额外的绑定。你都准备好了!

设置排序键