简体   繁体   English

使用NSArrayController对NSTableColumn进行排序

[英]Sorting NSTableColumn with NSArrayController

I have a NSArrayController bound to an NSTableView so the table column like the following: 我有一个NSArrayController绑定到NSTableView,所以表列如下所示:

NSTableView bindings:
Content -> ArrayController.arrangedObjects
SelectionIndexs -> ArrayController.arrangedObjects
SortDescriptors -> ArrayController.sortDescriptors

NSTableColumn bindings:
Value -> ArrayController.arrangedObjects.description

When I try and sort it using the column header it just crashes with something like 当我尝试使用列标题对它进行排序时,它会崩溃,例如

error setting value for key path sortDescriptors of object NSArrayController

Any ideas? 有任何想法吗?

I struggled with the exact same problem today. 我今天正为同样的问题而苦苦挣扎。

It seems that binding the content and selectionIndexes of the tableView to the array controller IB > inspector window > select your tableView > bindings tab , disables the sorting by clicking on the table header. 似乎将tableView的内容和selectionIndexes绑定到阵列控制器IB > inspector window > select your tableView > bindings tab ,可以通过单击表头来禁用排序。 This makes sense, because the table view now shows you the exact contents (and ordering) of the array controller. 这是有道理的,因为表格视图现在向您显示了阵列控制器的确切内容(和顺序)。

I unchecked these bindings in the IB, also removed any sort keys from the table columns IB > inspector window > select your NSTableColumn > attributes pane . 我在IB中取消选中了这些绑定,还从表列IB > inspector window > select your NSTableColumn > attributes pane删除了所有排序键。 Select the checkbox Creates Sort Descriptor in the table column's binding tab. 选中表格列的绑定选项卡中的Creates Sort Descriptor复选框。 No sortDescriptor is needed on the table, although I think binding the table's sortDescriptor to Shared User Defaults Controller saves the ordering when you quit your application. 尽管我认为将表的sortDescriptor绑定到“ Shared User Defaults Controller可以节省您退出应用程序时的排序,但是表上不需要sortDescriptor。

If you need to sort your table, put a sortDescriptor on the array controller, maybe in the awakeFromNib . 如果需要对表进行排序,请将sortDescriptor放在数组控制器上,也许放在awakeFromNib

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"propertyOfYourObject" ascending:YES selector:@selector(compare:)]]];
}

This does not interfere with clicking the table column headers. 这不会影响单击表列标题。

I couldn't get a sortDescriptor on the array controller to work with bindings. 我无法在数组控制器上获得sortDescriptor来处理绑定。

I see several issues in your binding attempt. 在您的绑定尝试中,我看到了几个问题。

  1. One doesn't usually need to bind the NSTableView at all. 通常不需要绑定NSTableView Binding the values of specific NSTableColumn s to the NSArrayController is enough. 将特定NSTableColumn的值绑定到NSArrayController就足够了。

  2. You try to bind something against a .description property. 您尝试将某些内容绑定到.description属性。 Please remember - " description " is like a "reserved word" in Obj-C. 请记住-“ 描述 ”就像是Obj-C中的“保留字”。 Any NSObject should present itself as NSString in its "description" method. 任何NSObject都应在其“ description”方法中以NSString出现。 This is what is called when you po <object> in the debugger, or pass an NSObject to NSLog via "%@". 当您在调试器中放置po <object>或通过“%@”将NSObject传递给NSLog时,这就是所谓的。 So... probably you'd want to rename your property to something else. 所以...可能您想将自己的属性重命名为其他名称。

  3. You do NOT need to bind the sort descriptors of the NSArrayController or the NSTableView or the NSTableColumn at all. 您根本不需要绑定NSArrayControllerNSTableViewNSTableColumn的排序描述符。 As it happens, when you bind an NSTableColumn 's value to the NSArrayController 's arrangedObjects , the NSTableColumn (actually the NSColumnHeader ) object knows to set the NSArrayController 's sortDescriptor to the same path as the one you specified for the column's value binding - as you click on the column header. 碰巧,当你绑定一个NSTableColumn的价值, NSArrayControllerarrangedObjectsNSTableColumn (实际上是NSColumnHeader )对象知道所设置的NSArrayControllersortDescriptor为你列的值绑定指定一个相同的路径-当您单击列标题时。 In other words - sorting by clicking on column headers comes free, if you just bind your column's value to the NSArrayController's arrangedObjects. 换句话说,如果您仅将列的值绑定到NSArrayController的rangedObjects,则通过单击列标题进行排序是免费的。

Documentation on table binding is bad and frustrating. 有关表绑定的文档不好而且令人沮丧。 There are several different schemes for working with a Table, and debugging binding problems is a real nightmare. 有多种使用表的方案,调试绑定问题是一个真正的噩梦。 However, there are plenty essays and tutorials on the net for this. 但是,网上有很多文章和教程。

Hope this helps. 希望这可以帮助。

Let me suggest you a simple way to do this - 让我建议您一个简单的方法-

NSTableColumn bindings:

Value -> 

Bind to: ArrayController

Controller Key : arrangedObjects

Model Key Path : keyPath (such as name)

If you are new to using bindings with table view, this article will be of great help to you- 如果您不熟悉在表视图中使用绑定,那么本文将对您有很大帮助-

EDIT: Project relocated to Github. 编辑:项目搬迁到Github。 (No more explanation - code only) (没有更多说明-仅代码)

NSTableView, NSArrayController and More Bindings NSTableView,NSArrayController和更多绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM