macos - NSTableView - 如何让自动排序和手动排序一起工作?

标签 macos swift cocoa sorting nstableview

我在 MyViewController 中有一个 NSTableView 并绑定(bind)了一个数组 Controller 。我希望能够通过单击表格列标题对其进行排序,它也应该自动排序。因此,对于自动排序,我在 IB 中设置了数组 Controller 的排序描述符绑定(bind),如下所示:

Bind to: MyViewController
Model Key Path: customSortDescriptors

在我添加的 MyViewController 类中:

var customSortDescriptors:[NSSortDescriptor] {
    let sd = NSSortDescriptor(key: "string", ascending: true, selector: "localizedStandardCompare:");
    return [sd]
}

对于 IB 中的相关表格列,我设置了:

Sort Key: string
Selector: caseInsensitiveCompare:

这一切正常,直到我在出现错误的地方单击表列标题:

Error setting value for key path customSortDescriptors of object NSAutounbinder: 0x60000005e060 - a nonretaining proxy for MyApp.MyViewController: 0x6000000e6600 (from bound object NSArrayController: 0x6000001c0b40[entity: GeneratedData, number of selected objects: 0]): [MyApp.MyViewController 0x6000000e6600 setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key customSortDescriptors.

我不确定为什么会收到此错误。有人能告诉我哪里出了问题吗?为什么 MyViewController 不符合键值编码?默认情况下,NSViews 和 NSViewControllers 不是兼容的吗?

除此之外,排序工作没有问题,但我在 Xcode 中抛出错误并想消除它。

最佳答案

我会被诅咒,但错误的原因显然是该属性是只读的。所以添加一个 setter,或者更好的是,将代码简单地更改为:

var customSortDescriptors = [NSSortDescriptor(key: "string", ascending: true, selector: "localizedStandardCompare:")];

修复了错误,自动和手动排序都有效。

关于macos - NSTableView - 如何让自动排序和手动排序一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30814869/

相关文章:

c - 将多维数组传递给 C 中的函数时出现段错误

linux - 从同步任务(launchdaemon)中卸载 LaunchAgent

ios - 转换为 Xcode 7 后 Alamofire + SwiftyJSON 编译错误

iOS/WatchKit : Is there a way for the watch kit app(swift) to use the iOS code (Objective C)?

如果鼠标在行上,则快速取消隐藏一列中的按钮

objective-c - 调用其实例由另一个类拥有的类的方法

windows - 在 OSX 上使用 Qt 保持领先地位

swift - NSSearchField TextDidChange 的方法是什么?

ios - didSelectRowAtIndexPath 带有包含部分的 tableView

objective-c - Cocoa/Obj-C 简单 XML 文件阅读器 - 需要帮助