Question

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
} 
Was it helpful?

Solution

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 (with UIImage's +imageNamed: method.)

You can get the NSTableColumn * parameter for this method using the -tableColumnWithIdentifier: method on NSTableView. Set the idenitfier in the column using the identifier attribute in Interface Builder (see below)

Xcode interface builder screenshot

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top