Sort NSTable and NSMutableArray

I have an NSTableView that I am trying to sort. The data source is NSMutableArray, which contains instances of custom classes.

-(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange: (NSArray *)oldDescriptors 

I use the above to track when a user clicks on a table title. For sorting, I use the following method:

 NSSortDescriptor *sortDescriptor; sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"make" ascending:bool_asc_desc] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSArray *sortedArray; sortedArray = [ads_printers_array sortedArrayUsingDescriptors:sortDescriptors]; 

What I would like to know is how can I pass in the column of the table that was clicked? This would allow me to modify initWithKey: @ "make" (using if statements) so that I can sort according to the header clicked.

Thanks.

PS I tried the following:

 if ([ads_rdp_driver_table selectedColumn] == tc_make) 

with ads_rdp_driver_table being my NSTableView and tc_make being the NSTableColumn that I defined. However, I get this error:

ISO C ++ prohibits comparison between pointer and integer

I think it might work if I can figure out the error.

+4
source share
1 answer

The selectedColumn NSTableView method returns the index of the selected column (and not a pointer to it), so you get a compilation error. To get the actual column, try something like this (my code contains errors, Im typing verbatim):

 NSTableColumn * selectedColumn = (NSTableColumn *)[[tableView tableColumns] objectAtIndex: [tableView selectedColumn]]; NSLog(@"Column identifier: %@", [selectedColumn identifier]); 
+3
source

Source: https://habr.com/ru/post/1415134/


All Articles