Sort NSTableView

I have two coloumn in NSTableView as a name and salary with 5-10 values. I want to sort these coloumn after clicking on the header of both columns. There is a lot of data on the Internet, but I cannot use it. Please help me do this in cocoa.

Thanks in advance and appreciate any help.

+7
source share
2 answers

Each table column has a setSortDescriptorPrototype method. setSortDescriptorPrototype descriptors are ways to tell the array how to sort itself (ascending, descending, case-insensitive, etc.) Iterate over each column that you want to sort, and call this method for each of these columns and pass the required sort descriptor (in my case, I will use the column identifier)

 for (NSTableColumn *tableColumn in tableView.tableColumns ) { NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:tableColumn.identifier ascending:YES selector:@selector(compare:)]; [tableColumn setSortDescriptorPrototype:sortDescriptor]; } 

After writing this piece of initialization code, NSTableViewDataSource has a method - (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors , which notifies you of every change to the sort descriptor, implements this method in the data source, and sends a message to the data array to sort yourself

 - (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors { self.data = [self.data sortedArrayUsingDescriptors:sortDescriptors]; [aTableView reloadData]; } 

This method works every time you click on the column heading, and NSTableColumn shows a nice little triangular button showing the sort order.

+16
source

I stumbled upon this question, looking for the easiest way to implement something like this. Although the original question is old, I hope someone finds my answer helpful! Please note that I am using Xcode 5.1.1

Ok, so for this you need:

  • select the actual column that you want to sort in your table.
  • In your attribute inspector, you need to fill in two fields: the sort key and the selector.
  • In the "Sort key" field, you need to enter the value of your Identifier. The value of your ID is in your ID.
  • In the "Selector" field, you need to enter the appropriate selection method based on the type of object in the column. Default Method: Compare:

Based on table programming guide for Mac. The compare: method works with NSString, NSDate, and NSNumber objects. If the table column contains only rows, you might want to use the caseInsensitiveCompare: if method if case sensitivity is not relevant. However, consider replacing these method signatures with localizedCompare: or localizedCaseInsensitiveCompare: methods to accommodate language requirements for users.

Finally, you need to declare a tableView: sortDescriptorsDidChange: method in your Table View Controller in the format shown below:

 -(void)tableView:(NSTableView *)mtableView sortDescriptorsDidChange:(NSArray *)oldDescriptors { [listArray sortUsingDescriptors: [mtableView sortDescriptors]]; [tableView reloadData]; } 
+7
source

All Articles