2008-12-23 91 views
4

我有一个可可应用程序,它通过一个NSArrayController获得了一个绑定到模型的TableView。如何为NSTableView设置默认排序顺序?

该应用程序按我的意愿工作,但该表的默认排序顺序错误。

buildwatch http://public.west.spy.net/BuildWatch.png

我通常启动该程序,然后单击最后一个头两次得到它排序的正确方法。在nib/bindings /中有没有指定默认排序顺序的方法,或者编程方式告诉它如果在那里点击两次会发生什么?甚至只记得以前的排序顺序?

回答

7

看看NSSortDescriptor

您可以在NSTableView上使用-setSortDescriptors:进行设置。或者,您可以将排序描述符放入ivar中,并将它们与IB中的Sort Descriptor绑定绑定。

5

我通常在-windowDidLoad中做这种事情。假设你NSWindowController子类有IBOutlet中_arrayController设为NSArrayController的问题,并且您的模型拥有的财产buildETA:

NSSortDescriptor *buildETASortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"buildETA" ascending:NO]; 
[_arrayController setSortDescriptors:[NSArray arrayWithObject:buildETASortDescriptor]]; 
[buildETASortDescriptor release]; 

编辑:改变-awakeFromNib到-windowDidLoad,因为这是一个假设NSWindowController

+1

钍似乎没有更新视图来表明它是按这种方式排序的。 – Dustin 2008-12-23 05:26:27

+0

谢谢!为我工作,为我更新了视图。 – ssj 2011-02-21 09:18:06

0

您还可以创建这个自定义的类,然后在InterfaceBuilder中选择您ArrayController,并在恒等式督察更改自定义类到您CustomArrayController

#import "CustomArrayController.h" 
@implementation PatchbayArrayController 

-(void)awakeFromNib 
{ 
    [super awakeFromNib]; 
    NSSortDescriptor *mySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"propertyNameHere" ascending:YES]; 
    [self setSortDescriptors:[NSArray arrayWithObject:mySortDescriptor]]; 
    [mySortDescriptor release]; 
} 
@end