NSTableView: programatically changing sorted column
NSTableView: programatically changing sorted column
- Subject: NSTableView: programatically changing sorted column
- From: Graham Cox <email@hidden>
- Date: Tue, 6 Jul 2010 11:50:45 +1000
I want to programatically change the column on which items are sorted in NSTableView.
When I call -setHighlightedTableColumn:, it does change the highlighted column, but annoyingly, nothing is done with the table's sort descriptors. I can't see any API for triggering a proper update to the sort descriptors as if I'd actually clicked the table column, so I'm trying to manage the sort descriptors myself, but so far this only half works (i.e. it's buggy).
I get a mutable copy of the current sort descriptors and attempt to find the desired column's sort descriptor and move it to the front of the list. That partially works. I also wondered if the descriptors took a copy of the column's prototype sort descriptor so I also tried looking for the selected sort descriptor by key. That didn't work at all.
Can someone tell me how I can achieve this? It seems ridiculously hard.
Note that I want to do this for good reason: I have two alternative views of some data, a table view and an icon view. The icon view is sorted to match the table view, so I rely on the table's sort descriptors for both views. When I change sorting in the icon view I need the table view to remain in synch, so the most straightforward way seems to be to simply change the highlighted column of the (hidden) table view which triggers an update by notifying that the sort descriptors have changed (except that it doesn't).
Code:
[mItemsListView setHighlightedTableColumn:column];
NSSortDescriptor* sd = [column sortDescriptorPrototype];
// just switching the column isn't enough - the sort descriptors must be updated ourselves
// (grr, why doesn't the table view do this?)
NSMutableArray* sortDescs = [[mItemsListView sortDescriptors] mutableCopy];
if([sortDescs containsObject:sd])
{
[sd retain];
[sortDescs removeObject:sd];
[sortDescs insertObject:sd atIndex:0];
[sd release];
// works but is buggy - table gets out of synch sometimes
}
else
{
// <sd> might have been copied so look for an existing sd having the same key
NSEnumerator* iter = [sortDescs objectEnumerator];
NSSortDescriptor* sdCopy;
while(( sdCopy = [iter nextObject]))
{
if([[sdCopy key] isEqualToString:[sd key]])
{
[sdCopy retain];
[sortDescs removeObject:sdCopy];
[sortDescs insertObject:sdCopy atIndex:0];
[sdCopy release];
break;
// doesn't work at all
}
}
}
[mItemsListView setSortDescriptors:sortDescs]; // triggers the notification which updates the views
[sortDescs release];
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden