views:

354

answers:

1

Hi, I have a NSTableView with 3 columns. I can sort using any of the colunms by clicking on the column header. Here is my issue though: I am presorting the array and then loading it in NSTableView so the initial TableView is already sorted by the values in one of the 3 columns. Now when I click on the column headers, i can resort and the small ascending/descending image ( triangle) appears indicating how the sort order is. What I want is, to be able to display this ascendingimage triangle in the column header right at the start when the NSTableView is loaded the first time, for the column based on which I have already sorted the Array. Thanks in advance :)

Thanks Peter and cb160. So I added this in my refreshList method:

The *lastColumn parameter has the right identifier if i display it using NSLog, but still that triangle image does not get loaded when the table loads data the first time. Is there something I am missing here ? My table View is setup like this:

-(IBAction)refreshList:(id)sender
{
//setup array here and sort the array based on one column. This column has 
     identifier 'col1' and it works as expected

    // trying to set the sortindicator image for the column here
    NSTableColumn *lastColumn;

lastColumn = [aTableView tableColumnWithIdentifier:@"col1"];
[aTableView setIndicatorImage:[NSImage imageNamed:@"NSDescendingSortIndicator"]     
inTableColumn:lastColumn];
[aTable reloadData];
 } 

 - (int) numberOfRowsInTableView:(NSTableView *)aTable
{ // return count of array
 }

- (id)tableView:(NSTableView *)aTable objectValueForTableColumn: (NSTableColumn *)          
   tableColumn row:(int)row
  { 
   //set up arrays here to load data in each column


  }
- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray   
  *)oldDescriptors
{
//sort here when column headers are clicked
} 
A: 

You can use the setIndicatorImage:inTableColumn method on NSTableView to do this.

You can use the strings NSAscendingSortIndicator and NSDescendingSortIndicator to use the built in images

You can get the NSTableColumn* parameter for this method using the

(NSTableColumn *)tableColumnWithIdentifier:(id)anObject method on NSTableView. Set the idenitfier in the column using the identifier attribute in Interface Builder (see below)

alt text

Robert Christie
Thats the thing. I am not sure how to select the right Column.eg: [aTable setIndicatorImage:[NSImage imageNamed:@"NSAscendingSortIndicator"] inTableColumn:tableColumn];How do I actually get the right Tablecolumn when I have multiple columns ?
califguy
Give them identifiers in IB, then ask the table view for the column with that identifier.
Peter Hosey
Tried that. eg:[aTable setIndicatorImage:[NSImage imageNamed:@"NSAscendingSortIndicator"] inTableColumn:(NSTableColumn *)[aTable columnWithIdentifier:@"col1"] ];where col1 is the identifier for that column. And i do a [tableview reloaddata] right after that. But still cannot get that triangle to show when tableView loads the first time.
califguy
@Peter Hosey: Added examples on getting column with identifier.
Robert Christie
added more details and code to my orig. question. Thanks a lot guys for the help. I can get the identifier for the column now but still cannot set the sort indicator image. Anything I am missing in the code above ?
califguy
@califguy: Try removing the reload data - perhaps that is resetting the sort order. Load the data before setting the sort order
Robert Christie