I'm loading a CSV file that I know nothing about except that the first row are column headers, which could be anything. The number of columns is unknown too. I tried to add a sortDescriptorPrototype to each column and when I do I get up/down arrows visible in the TableView.
for(NSTableColumn * col in self.tableView.tableColumns){
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: col.title ascending: YES];
col.sortDescriptorPrototype = sortDescriptor;
}
Each time I click on the column sortDescriptorsDidChange is called:
- (void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
[_data sortUsingDescriptors: [tableView sortDescriptors]];
[tableView reloadData];
}
_data is a NSMutableArray where data is stored and first row are the header names. Each row is another NSArray Unfortunately I get this error:
2017-04-17 12:35:09.979142+0200 Table Tool[20954:2577488] [General] [<__NSCFString 0x610000474840> valueForUndefinedKey:]: this class is not key value coding-compliant for the key beds.
2017-04-17 12:35:09.982161+0200 Table Tool[20954:2577488] [General] (
0 CoreFoundation 0x00007fffb31a837b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffc7f9c48d objc_exception_throw + 48
2 CoreFoundation 0x00007fffb31a82c9 -[NSException raise] + 9
3 Foundation 0x00007fffb4c83e5e -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226
4 Foundation 0x00007fffb4b55d9c -[NSObject(NSKeyValueCoding) valueForKey:] + 283
5 Foundation 0x00007fffb4b59aac -[NSArray(NSKeyValueCoding) valueForKey:] + 467
6 Foundation 0x00007fffb4b55b79 -[NSArray(NSKeyValueCoding) valueForKeyPath:] + 448
7 Foundation 0x00007fffb4b7cc9f _sortedObjectsUsingDescriptors + 371
8 Foundation 0x00007fffb4be94f7 -[NSMutableArray(NSKeyValueSorting) sortUsingDescriptors:] + 468
9 Table Tool 0x00000001000051d8 -[Document tableView:sortDescriptorsDidChange:] + 184
10 AppKit 0x00007fffb0f05425 -[NSTableView setSortDescriptors:] + 260
11 AppKit 0x00007fffb13c9fbc -[NSTableView _changeSortDescriptorsForClickOnColumn:] + 480
Obviously I have read a lot on the topic, but all examples assume that you know column names at design-time. I'm interested in a solution with NSSortDescriptor and sortUsingDescriptors in Obj-C or Swift, some explanation on sortDescriptorPrototype and why you need it in the first place, since sortDescriptor changes when I click on the column. Is there anything in KVC I can use for the sorting key, like "self" (there is ...valueForKeyPath:@"@sum.self"...)?
Some useful links:
https://www.raywenderlich.com/143828/macos-nstableview-tutorial