Reputation: 616
I've a cell-based table bound via an array controller to a core data entity with a date field, and a column displaying the date, all created in InterfaceBuilder without any coding. I can click on the column header to get the data sorted by date, without any coding or even any further binding. How can I get this behaviour for a view-based table? Have I got to resort to some coding?
The date is displayed in a text field bound to the Table Cell View with key objectValue.date.
Upvotes: 4
Views: 2802
Reputation: 5299
Make sure to bind the table's Sort Descriptors binding to the array controller's sortDescriptors
property.
Upvotes: 2
Reputation: 49
this is a bit late but you didn't have to write any line of code. Instead, go to the attribute inspector of the column, and set the "Sort key" field to "date" (without quotes). The selector field will automatically be set to "compare".
EDIT:it turns out you don't even have to do that. Bind the table column's value to the property by which you want rows to be sorted, just as you would for a cell-based table, and that's it. This way, you don't bind the entire table content to the array controller's arranged objects, which contradicts the Apple guide on view-based tables. At least it worked for me when I first created and bound a cell-based table, which I then converted to view-based table (in the attribute inspector, Xcode 4.5.2, OS X 10.7.5). I tried that by starting with a view-based table and THEN binding the columns, but sorting didn't work (it may just be some error I did).
Upvotes: 1
Reputation: 616
Maybe no-one's using these newfangled view-based tables, or maybe you're all flummoxed. Three weeks on, I've stumbled on a solution, not quite codeless, which works for me, so I think I'd better share it.
The secret is the table's Sort Descriptors binding, which you must bind to an array of NSSortDescriptors. Obvious in hindsight. Moral: try to understand what it says on tin! Naïvely I thought this was where I could define a sort descriptor or bind to some sortable object, Interface Builder being a design-time thing. Of course nothing worked. Continuing the example, here's what to do.
Declare an array property. I did this in my app delegate.
@property (assign) NSArray *dateSorters;
and synthesize it. In OS 10.7 you shouldn't need a matching ivar. Initialize it. I did this in applicationDidFinishLaunching:
dateSorters = [NSArray arrayWithObject:
[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]];
That is the only line of active code. Now the bindings. Bind your array controller's Sort Descriptors to your app delegate, with the Model Key Path dateSorters. If it doesn't appear in the list of possibles, try doing a build. The table will now appear sorted. To make clicking on the column header work, you must bind the table's Sort Descriptors to the same array, and you must associated the column with the date field: with the date column selected, bind its value to your array controller, arrangedObjects with the Model Key Path date, and check Creates Sort Descriptor. That is the same as you would have done for a cell-based table. Clicking the date column header now toggles the sort order. Clicking other column headers, as intended, does nothing.
One tiny problem left - on its first appearance, the table is unsorted.
Upvotes: 4