如何使用多个排序描述符在列上设置指标图像?

时间:2010-08-30 12:43:38

标签: cocoa nstableview nsarraycontroller nssortdescriptor nstablecolumn

NSDisableScreenUpdates();

[self.productsArrayController setFetchPredicate:predicate];
[self.productsArrayController setSortDescriptors:sortDescriptors];

[self.productsArrayController fetchWithRequest:[self.productsArrayController defaultFetchRequest] merge:YES error:nil];

NSSortDescriptor *lastSortDescriptor = [sortDescriptors lastObject];
[tableView setHighlightedTableColumn:[tableView tableColumnWithIdentifier:[lastSortDescriptor key]]];
[tableView setIndicatorImage:[NSImage imageNamed: ([lastSortDescriptor ascending]) ? @"NSAscendingSortIndicator" : @"NSDescendingSortIndicator"]
                          inTableColumn:[tableView tableColumnWithIdentifier:[lastSortDescriptor key]]];        

NSLog(@"currentImage: %@", [tableView indicatorImageInTableColumn:[tableView tableColumnWithIdentifier:[lastSortDescriptor key]]]); /* DEBUG LOG */

NSEnableScreenUpdates();

上面的代码没有设置三角形排序图像。我正在设置多个排序描述符,但在将其更改为单个描述符后,它会开始在列标题中显示正确的图像。 在这两种情况下,currentImage都显示我设置了正确的图像(降序相同):

currentImage: <NSImage 0x200495fc0 Name=NSAscendingSortIndicator Size={9, 9} Reps=(
    "NSBitmapImageRep 0x20049f480 Size={9, 9} ColorSpace=Generic RGB colorspace BPS=8 BPP=32 Pixels=9x9 Alpha=YES Planar=NO Format=0 CurrentBacking=<CGImageRef: 0x2004a9e80>"

更改上述方法中的操作顺序并将reloadData发送到我的tableView,对结果没有影响。

在我看来,setIndicatorImage按预期工作,但不知何故图像没有显示在标题中,或者至少不可见。

我在这里缺少什么?

修改

代码用于通过以下方式将NSViewController的子类放在应用程序窗口的主视图中:

// adding view inside the main view
[mainView addSubview:view];
[view setFrame:[mainView bounds]];

[view display];
[mainView display];

1 个答案:

答案 0 :(得分:1)

好的,我刚刚发现了一些简单的解决办法:

[[[tableView tableColumnWithIdentifier:[lastSortDescriptor key]] headerCell] 
setStringValue:[columnName stringByAppendingString:([lastSortDescriptor ascending]) ? @"  ▲" : @"  ▼"]];
相关问题