I created a custom NSTableViewHeader
. Everything works fine, but I don't find a simple way to get the "key" for the current column to check if the sort indicator should be drew.
Here the core code to check for the sort indicator:
// Check if we need to draw a sort descriptor
for (NSInteger priority = 0; priority < self.tableView.sortDescriptors.count; ++priority) {
NSSortDescriptor *sortDesciptor = self.tableView.sortDescriptors[priority];
if ([sortDesciptor.key isEqualTo:???????]) {
[tableHeaderCell drawSortIndicatorWithFrame:rect inView:self ascending:sortDesciptor.ascending priority:priority];
}
}
To see everything in context, here the whole drawRect
code:
- (void)drawRect:(NSRect)dirtyRect
{
[NSGraphicsContext saveGraphicsState];
// Fill the background
[_backgroundColor setFill];
const CGRect headerRect = CGRectMake(0.0, 1.0, self.bounds.size.width, self.bounds.size.height-1.0);
[[NSBezierPath bezierPathWithRect:headerRect] fill];
// Draw separators and column header text.
for (NSUInteger i = 0; i < self.tableView.numberOfColumns; ++i) {
NSRect rect = [self headerRectOfColumn:i];
if (i != 0) {
[[NSColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.35] setFill];
[[NSBezierPath bezierPathWithRect:NSMakeRect(rect.origin.x, rect.origin.y+4.0, 1.0, rect.size.height-8.0)] fill];
}
NSTableColumn *tableColumn = self.tableView.tableColumns[i];
NSTableHeaderCell *tableHeaderCell = tableColumn.headerCell;
NSString *columnText = tableHeaderCell.stringValue;
[columnText drawInRect:NSInsetRect(rect, 5.0, 4.0) withAttributes:_headerTextAttributes];
// Check if we need to draw a sort indicator
for (NSInteger priority = 0; priority < self.tableView.sortDescriptors.count; ++priority) {
NSSortDescriptor *sortDesciptor = self.tableView.sortDescriptors[priority];
if ([sortDesciptor.key isEqualTo:tableColumn.key]) {
[tableHeaderCell drawSortIndicatorWithFrame:rect inView:self ascending:sortDesciptor.ascending priority:priority];
}
}
}
[NSGraphicsContext restoreGraphicsState];
}
How can I find the "key" for the current column to check if the sort indicator shall be drew?
The only way which worked for me is checking the bindings of the NSTableColumn
. This is working for me, because I use a NSArrayController
. My NSTableView
is using a cell based layout and each NSTableColumn
is bound to the arrangedObjects.xxx
key path.
// Check if we need to draw a sort descriptor
for (NSInteger priority = 0; priority < self.tableView.sortDescriptors.count; ++priority) {
NSSortDescriptor *sortDesciptor = self.tableView.sortDescriptors[priority];
NSDictionary *bindingInfo = [tableColumn infoForBinding:@"value"];
NSString *keyPath = bindingInfo[NSObservedKeyPathKey];
keyPath = [keyPath stringByReplacingOccurrencesOfString:@"arrangedObjects." withString:@""];
if ([sortDesciptor.key isEqualTo:keyPath]) {
[tableHeaderCell drawSortIndicatorWithFrame:rect inView:self ascending:sortDesciptor.ascending priority:priority];
}
}
But this won't work for view based tables. Is this really the only solution?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.