Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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:
Code:
- (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:
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

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
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

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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 :eek:) and try and compress the code into one line you get very nasty code that's practically unreadable ;).
 

roberthuttinger

macrumors newbie
Sep 24, 2008
8
0
so this goes into the data.h/m file?

PHP:
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

Moderator emeritus
Jun 15, 2000
7,958
7
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:

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

roberthuttinger

macrumors newbie
Sep 24, 2008
8
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.