在NSOutlineView子类中丢失选择突出显示

时间:2012-03-06 18:12:17

标签: objective-c xcode macos nsoutlineview

要自定义默认的蓝色渐变高亮显示样式,我创建了NSOutlineView子类并覆盖方法-highlightSelectionInClipRect,如下所示:

- (void)highlightSelectionInClipRect:(NSRect)theClipRect
{
    NSRange aVisibleRowIndexes = [self rowsInRect:theClipRect];
    NSIndexSet *aSelectedRowIndexes = [self selectedRowIndexes];
    NSInteger aRow = aVisibleRowIndexes.location;
    NSInteger anEndRow = aRow + aVisibleRowIndexes.length;

    for (int aRow; aRow < anEndRow; aRow++) {
        if([aSelectedRowIndexes containsIndex:aRow]) {
            // draw gradient
        }
    }
}

这样可以正常工作,但有时候不会绘制背景。在下面的屏幕截图中,您可以看到在选择最后一个项目后单击第一个项目时如何不绘制选择突出显示。

Loosing selection highlight on step 4

如果新选择的项目不直接位于旧选定的之下或之上,似乎只会发生这种情况。在1-2-3-4-5-4-3-2-1的顺序中选择五个项目总是绘制适当的背景,其他任何东西(例如1-2-5)都没有。

为什么会这样?如果您需要更多详细信息,我将很乐意添加更多代码,但与此同时,我不知道在哪里搜索此行为。

1 个答案:

答案 0 :(得分:1)

这是我使用块的非常简单的解决方案:

- (void)highlightSelectionInClipRect:(NSRect)clipRect
{
    [[self selectedRowIndexes] enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) 
     {
         // draw gradient
    }];
}
相关问题