2008-12-23 93 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