PDA

View Full Version : Sorting NSTableView




stadidas
Feb 12, 2007, 07:23 PM
Hi everyone.

I currently have a NSTableView set up, and it's data source is an instance of NSMutableArray called "logs". I have these data source methods implemented at the moment:

- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSString *identifier = [aTableColumn identifier];
Log *log = [logs objectAtIndex:rowIndex];
return [log valueForKey:identifier];
}

- (void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSString *identifier = [aTableColumn identifier];
Log *log = [logs objectAtIndex:rowIndex];
[log setValue:anObject forKey:identifier];
}

- (void)tableView:(NSTableView *)aTableView
sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
NSArray *newDescriptors = [tableView sortDescriptors];
[logs sortUsingDescriptors:newDescriptors];
[tableView reloadData];
}


They all seem to work well enough as the data is loaded into the table correctly as you can see here:
http://www.matefeedkillrepeat.com/images/logsheet.png

However, when I click a column header, the data is not re-sorted based on that column. I have used the method in Hillegass' book, but it doesn't seem to work for me. If anyone could give me a hand it would be immensely helpful.



caveman_uk
Feb 13, 2007, 02:43 AM
For what you're doing (ie nothing unusual) it looks like Cocoa Bindings would be a better idea - they support column sorting without you having to write any extra code.

stadidas
Feb 13, 2007, 01:14 PM
For what you're doing (ie nothing unusual) it looks like Cocoa Bindings would be a better idea - they support column sorting without you having to write any extra code.
I thought about that but I'm not quite sure about how to implement it. The other data my app uses is stored in a model class called details, along with the "logs" NSMutableArray. I currently have an instance of NSObjectController controlling the flow of data between these data items and the UI. I have added the "logs" key to the attributes of the NSObjectController and have set the NSTableView's data source to this key value, but know data displays in the table. In the Hillegass book he uses NSArrayController to send data to a tableView, but I'm not sure how to implement this when I already have an NSObjectController interacting with my model class.
Any ideas?

Eraserhead
Feb 13, 2007, 02:21 PM
If you are using a table data source (in AppController.m) you simply need to sort the array before returning the appropriate value using the sortedArrayUsingDescriptors: method on NSArray.

I would give an example but if you are an idiot (like me :o) and try and compress the code into one line you get very nasty code that's practically unreadable ;).

roberthuttinger
Nov 20, 2008, 03:48 PM
NSSortDescriptor *lastNameSorter = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];

but at what point and how does the sortdescriptor know its working on an array, just by the fact its working with a key?

cheers.bo

wyzfam.com
:apple:

kainjow
Nov 20, 2008, 07:33 PM
If you're doing this without bindings...

First you need to setup your table columns with setSortDescriptorPrototype:

Then you need to implement the table view delegate method tableView:sortDescriptorsDidChange:. Here's an example:

- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
[myMutableArray sortUsingDescriptors:[aTableView sortDescriptors]];
[aTableView reloadData];
}

roberthuttinger
Nov 20, 2008, 09:47 PM
Perfect that makes complete sense! I was trying to do it in the data.m file and it was breaking a function I had. Thanks fir the post!

Rob
WyzFam : tMiMP : tCiMP : My Baby Signs