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]];
}
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.