使NSTableView列可通过Interface Builder进行排序

时间:2018-04-11 18:57:10

标签: swift4 nstableview xcode9

我有一个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")]

}

我已在NSArrayController的视图控制器中添加了NSTableviewMain.storyboard。阵列控制器"控制器内容"使用模型关键路径self.fooList绑定到视图控制器,表格视图"表格内容"使用默认数据(arrangedObjects的控制器密钥)和每个表视图单元"值"绑定到数组控制器。使用"模型密钥路径"绑定到表单元格视图。分别为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)  

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

1 个答案:

答案 0 :(得分:4)

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

对于表格视图:

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

Bind table's Sort Descriptors

对于表格列:

为“名称”列设置排序键=名称,为“颜色”列设置排序键=颜色。不需要额外的绑定。你们都准备好了!

Set Sort Key

相关问题