Re: NSTableView, NSArrayController and secondary sortings
Re: NSTableView, NSArrayController and secondary sortings
- Subject: Re: NSTableView, NSArrayController and secondary sortings
- From: mmalc crawford <email@hidden>
- Date: Sun, 9 Dec 2007 10:51:40 -0800
On Dec 8, 2007, at 8:00 AM, Adhamh Findlay wrote:
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.
Taking this literally (i.e. it doesn't matter what other sorting there
is, the date sort should always be second), you can create a subclass
of NSArrayController that adds a date sort descriptor in the correct
place, along the lines of the example shown below.
mmalc
@interface SortByDate2ArrayController : NSArrayController
{
NSMutableArray *sortDescriptors;
}
@end
@implementation SortByDate2ArrayController
- (NSArray *)sortDescriptors
{
return sortDescriptors;
}
- (void)setSortDescriptors:(NSArray *)newDescriptors
{
if (sortDescriptors == newDescriptors)
{
return;
}
[sortDescriptors release];
sortDescriptors = [newDescriptors mutableCopy];
NSSortDescriptor *dateDescriptor = nil;
for (NSSortDescriptor *descriptor in newDescriptors)
{
if ([[descriptor key] isEqualToString:@"date"])
{
dateDescriptor = descriptor;
[sortDescriptors removeObject:descriptor];
}
}
if (dateDescriptor == nil)
{
dateDescriptor = [[[NSSortDescriptor alloc]
initWithKey:@"date" ascending:YES] autorelease];
}
NSInteger dateIndex = 1;
if ([sortDescriptors count] == 0)
{
dateIndex = 0;
}
[sortDescriptors insertObject:dateDescriptor atIndex:dateIndex];
[self rearrangeObjects];
}
@end
_______________________________________________
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