0
我創建了一個自定義NSTableViewHeader
。一切工作正常,但我沒有找到一個簡單的方法來獲取當前列的「關鍵」,以檢查是否應該繪製排序指標。如何查找「NSTableColumn」的關鍵字來繪製排序指示符?
這裏的核心代碼檢查排序指標:
// 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];
}
}
要了解來龍去脈,這裏的整個drawRect
代碼:
- (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];
}
我如何才能找到「鑰匙」了當前列檢查是否應繪製排序指示符?