Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 12, 2007, 08:23 PM   #1
stadidas
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
Sorting NSTableView

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:


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.
stadidas is offline   0 Reply With Quote
Old Feb 13, 2007, 03:43 AM   #2
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: 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.
caveman_uk is offline   0 Reply With Quote
Old Feb 13, 2007, 02:14 PM   #3
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
Quote:
Originally Posted by caveman_uk View Post
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?
stadidas is offline   0 Reply With Quote
Old Feb 13, 2007, 03:21 PM   #4
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: 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 ) and try and compress the code into one line you get very nasty code that's practically unreadable .
Eraserhead is offline   0 Reply With Quote
Old Nov 20, 2008, 04:48 PM   #5
roberthuttinger
macrumors newbie
 
Join Date: Sep 2008
so this goes into the data.h/m file?

PHP Code:
NSSortDescriptor *lastNameSorter = [[NSSortDescriptor allocinitWithKey:@"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
roberthuttinger is offline   0 Reply With Quote
Old Nov 20, 2008, 08:33 PM   #6
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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];
}
kainjow is offline   0 Reply With Quote
Old Nov 20, 2008, 10:47 PM   #7
roberthuttinger
macrumors newbie
 
Join Date: Sep 2008
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
roberthuttinger is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
NSTableView get selected row data cpuin Mac Programming 2 Oct 2, 2013 12:08 PM
Edit NSTableView - NSArrayController kikko088 Mac Programming 0 Jan 17, 2013 09:57 AM
Call reloadData for NSTableView from AppDelegate AdrianK Mac Programming 3 Aug 11, 2012 02:32 PM
Reordering rows in a view-based NSTableVIew monsieurpaul Mac Programming 2 Jul 19, 2012 09:21 AM
Asynchronous NSURLConnections + NSTableView Help jwei92 Mac Programming 0 Jul 10, 2012 09:42 PM

Forum Jump

All times are GMT -5. The time now is 02:36 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC