Sorting NSTableView

Discussion in 'Mac Programming' started by stadidas, Feb 12, 2007.

  1. macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #1
    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:
    [​IMG]

    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.
     
  2. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    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.
     
  3. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #3
    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?
     
  4. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    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 ;).
     
  5. macrumors newbie

    Joined:
    Sep 24, 2008
    #5
    so this goes into the data.h/m file?

    PHP:
    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
    :apple:
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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];
    }
     
  7. macrumors newbie

    Joined:
    Sep 24, 2008
    #7
    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
     

Share This Page