How to set default sort order for NSTableView?

I have a cocoa application that got a TableView with model bindings via NSArrayController.

The application works the way I want, but the default sort order in the table is wrong.

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

Usually I run the program and double click on the last title to sort it correctly. Is there a way in nib / bindings / what to specify the default sort order or to tell it programmatically what happens if I double-click it? Or even remember the previous sort order?

+4
source share
3 answers

Take a look at NSSortDescriptor .

You can customize it with -setSortDescriptors: in NSTableView. Or you can put sort descriptors in ivar and bind them to the Sort Descriptor binding in IB.

+7
source

I usually do such things in -windowDidLoad. Suppose your subclass of NSWindowController has an IBOutlet _arrayController installed in the corresponding NSArrayController, and that your model has the buildETA property:

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

Edit: Changed -awakeFromNib to -windowDidLoad since this is a hypothetical subclass of NSWindowController

+5
source

You can also create this custom class, and then in InterfaceBuilder select your ArrayController and change the Custom Class in Indentity Inspector to your 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 
0
source

All Articles