Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a default sort order for an NSTableView?

Tags:

cocoa

I've got a cocoa app that's got a TableView with bindings to a model through an NSArrayController.

The app works as I want, but the default sort order of the table is wrong.

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

I typically start the program and click on the last header twice to get it sorting the right way. Is there a way in the nib/bindings/whatever to specify the default sort order, or to programatically tell it to do what would happen if I clicked there twice? Or even just remember the previous sort order?

like image 366
Dustin Avatar asked Dec 18 '22 09:12

Dustin


2 Answers

Look at NSSortDescriptor.

You can set it up using -setSortDescriptors: on the NSTableView. Or you can put the sort descriptors in an ivar and bind them with the Sort Descriptor binding in IB.

like image 113
Nathan Kinsinger Avatar answered Jan 09 '23 17:01

Nathan Kinsinger


I typically do this sort of thing in -windowDidLoad. Suppose your NSWindowController subclass has the IBOutlet _arrayController set to the NSArrayController in question, and that your model possesses the property buildETA:

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 NSWindowController subclass

like image 22
sbooth Avatar answered Jan 09 '23 17:01

sbooth