Sorting NSTableView

Hi,

I am setting an Sortdescriptor on an NSArrayController backed TableView. The "numeric" search works only sometimes (mostly after restarting the application one or twice).

Is there a better place to set the sortdescriptor?


Thx


Frank

- (void)viewDidLoad {


    [super viewDidLoad];
    // Do view setup here.
  
    // Datasource set for custom sorting only
    [self.eventsTableView setDataSource:self];
  
    // set a sortDescriptorProtoype to support custom sorting
    // Column number, a NSString in the model ist filled with numeric values (mostly)
    // therefore a numeric search
    NSSortDescriptor *numberSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"number"
                                                                           ascending:YES
                                                                          comparator:^(NSString *a, NSString *b) {
                                                                              return [a compare:b options:NSNumericSearch];
                                                                          }];
    NSTableColumn *tableColumn = [self.eventsTableView tableColumnWithIdentifier:@"number"];
    [tableColumn setSortDescriptorPrototype:numberSortDescriptor];
  
    // sort the tableview initially by number
    [self.eventsTableView setSortDescriptors:@[numberSortDescriptor]];
}
Answered by Ken Thomases in 74690022

Given that you're binding the table view's content to an array controller, you should also bind its sort descriptors to the array controller's sortDescriptors. Then, in -viewDidLoad, you should set the initial sortDescriptors of the array controller, not the table view. It's actually the array controller that does the sorting and dictates which order the data is presented in. A table view never actually sorts anything. It tracks the sort descriptors but it would be the data source or, in the case of bindings, the array controller which actually does the sorting.

Accepted Answer

Given that you're binding the table view's content to an array controller, you should also bind its sort descriptors to the array controller's sortDescriptors. Then, in -viewDidLoad, you should set the initial sortDescriptors of the array controller, not the table view. It's actually the array controller that does the sorting and dictates which order the data is presented in. A table view never actually sorts anything. It tracks the sort descriptors but it would be the data source or, in the case of bindings, the array controller which actually does the sorting.

Thanks, that did it.

I can also remove the

NSTableColumn *tableColumn = [self.eventsTableView tableColumnWithIdentifier:@"number"]; 
    [tableColumn setSortDescriptorPrototype:numberSortDescriptor];

now.

Setting the sort descriptor prototype of a table column allows the column to inform the table view of how it should be sorted when the user clicks that column's header. (Again, the table view isn't able to sort itself, but it informs the data source or, if so bound, its array controller, which sorts the data.)


It also allows the table view to show an indicator in the header of the primary-sorting column.


If your table doesn't have headers or will only ever have one column or isn't otherwise user-sortable, then you don't need to set the column's sort descriptor prototype.

Sorting NSTableView
 
 
Q