gpt4 book ai didi

swift4 - 通过 Interface Builder 使 NSTableView 列可排序

转载 作者:行者123 更新时间:2023-12-05 00:12:48 24 4
gpt4 key购买 nike

我有一个 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 的 View Controller 中添加了 NSTableviewMain.storyboard 。 Array Controller 的“Controller Content”通过 Model Key Path self.fooList 绑定(bind)到 View Controller ,Table View 的“Table Content”通过默认数据( arrangedObjects 的 Controller Key)绑定(bind)到数组 Controller ,每个 Table View Cell 的“值”分别使用 objectValue.nameobjectValue.colour 的“模型键路径”绑定(bind)到表格单元格 View 。到目前为止,一切正常,我得到了填充表格 View 的数据。

根据评论中的建议,为了使列表可排序,我正在尝试添加“排序描述符”绑定(bind)。我已将 TableView 的“排序描述符”绑定(bind)到数组 Controller (默认选项),并将数组 Controller 的排序描述符绑定(bind)到 View Controller (添加了 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 Bindings 的一个经典问题:当它起作用时,它就像魔法一样,但当它不起作用时,你不知道如何修复它。我假设您已经填充了表格内容,所以我不会触及它。以下是处理排序的方法:

对于表格 View :

将表格 View 的排序描述符绑定(bind)到数组 Controller 的 sortDescriptors :

Bind table's Sort Descriptors

对于表格列:

设置 排序键 = 名称 对于名称列和 排序键 = 颜色 对于颜色列。不需要额外的绑定(bind)。你都准备好了!

Set Sort Key

关于swift4 - 通过 Interface Builder 使 NSTableView 列可排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49782588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com