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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With