Re: NSTableView, NSArrayController and secondary sortings
Re: NSTableView, NSArrayController and secondary sortings
- Subject: Re: NSTableView, NSArrayController and secondary sortings
- From: Wdyp <email@hidden>
- Date: Sun, 9 Dec 2007 10:25:35 +0100
I have an NSTableView that is bound to an NSArrayController. There
are 5 columns in the table view, one of which is an NSDate. What I
would like to do is always have the secondary sort be the date
column. The default behavior seems to be that the secondary sort is
based on the previously selected column.
I would disable sorting by clicking the column header altogether.
For each column uncheck the Create sort descriptors binding option and
remove any key in the column inspector.
Then in your controller object create 4 arrays of sort descriptors:
"FirstName-Ascending + Date-Ascending", "LastName-Ascending + Date-
Ascending" and so on.
In IB, add a segmented control or something similar and have its
action set the sort descriptors of your array controller. Voilà. You
can even have your segmented control selectedTag binding set to a
value of user defaults to get the table already sorted as it was the
last time your app was run.
If you really want the user to be able to use the regular header
method, I guess it could be possible to use some delegate method to
handle the single click in the header, swap the sort descriptors then
highlight by hand the header. But I can’t help you with this. Besides,
current cocoa header highlighting scheme will never indicate precisely
which is the secondary key whereas the segmented control titles or
icons can be very clear to the user.
As I frequently create sort descriptors arrays, I added the following
methods in a category of NSSortDescriptor.
Flofl.
+(NSArray *)ascendingDescriptorsForKey:(NSString *)key {
NSSortDescriptor * tempDescriptor = [[[NSSortDescriptor alloc]
initWithKey:key
ascending:YES] autorelease];
return [NSArray arrayWithObject: tempDescriptor];
}
+(NSArray *)ascendingDescriptorsForKeys:(NSString *)firstKey,... {
NSMutableArray * tempArray = [NSMutableArray arrayWithCapacity:3];
NSSortDescriptor * tempDescriptor;
va_list keysList;
id key;
if (firstKey) {
// First key:
tempDescriptor = [[[NSSortDescriptor alloc]
initWithKey:firstKey
ascending:YES] autorelease];
[tempArray addObject:tempDescriptor];
// Other keys:
va_start(keysList, firstKey);
while (key = va_arg(keysList, id)) {
tempDescriptor = [[[NSSortDescriptor alloc]
initWithKey:key
ascending:YES] autorelease];
[tempArray addObject:tempDescriptor];
}
va_end(keysList);
}
return [NSArray arrayWithArray:tempArray];
}_______________________________________________
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