Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a NSArrayController backed NSTableView

I have a NSArrayController and a NSTableView. They show tracks from iTunes. I can sort the list by clicking in the header.

Is there a way to set up a default sort descriptor for the table view so it sorts for albums every time the user launches the app?

I tried to set the sortDescriptor on the array controller and the table view but that changes nothing.

Thank you

Edit: The answer is right. But it needs a NSArray:

- (NSArray *)mainSortDescriptor {

    return [NSArray arrayWithObjects:
            [NSSortDescriptor sortDescriptorWithKey:@"album" ascending:YES], 
            [NSSortDescriptor sortDescriptorWithKey:@"trackNumber" ascending:YES], 
            nil];

}

like image 370
david Avatar asked Mar 29 '11 20:03

david


1 Answers

If you want to bind the array controller's sort descriptor, you have to bind it to something. You can put this in your application delegate, for example:

- (NSArray *)tracksSortDescriptors {
    return [NSArray arrayWithObject:
             [NSSortDescriptor sortDescriptorWithKey:@"albumName"
                                           ascending:YES]];
}

Then you can set up the binding in IB as

Bind to: MyAppDelegate  
Model Key Path: tracksSortDescriptors

EDITED. I forgot, when translating this from PyObjC, that I was returning a list. Oops.

like image 169
jscs Avatar answered Oct 29 '22 05:10

jscs