gpt4 book ai didi

objective-c - NSOutlineView 更改组行颜色

转载 作者:行者123 更新时间:2023-12-03 16:33:38 24 4
gpt4 key购买 nike

在我的 NSOutlineview 中,我使用的是 NSTextFieldCell 的子类的自定义单元格,我需要为组行和普通行绘制不同的颜色,当它被选择时,

为此,我做了以下操作,

-(id)_highlightColorForCell:(NSCell *)cell
{
return [NSColor colorWithCalibratedWhite:0.5f alpha:0.7f];
}

是的,我知道它的私有(private) API,但我找不到任何其他方法,这对于普通行效果很好,但对组行没有影响,有没有办法改变组颜色,

亲切的问候罗汉

最佳答案

您实际上可以在不依赖私有(private) API 的情况下做到这一点,至少如果您愿意需要 Mac OS X 10.4 或更高版本。

将以下内容放入您的单元格子类中:

- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
// Returning nil circumvents the standard row highlighting.
return nil;
}

然后子类化 NSOutlineView 并重新实现该方法, - (void)highlightSelectionInClipRect:(NSRect)clipRect;

以下示例为非组行绘制一种颜色,为组行绘制另一种颜色

- (void)highlightSelectionInClipRect:(NSRect)clipRect
{
NSIndexSet *selectedRowIndexes = [self selectedRowIndexes];
NSRange visibleRows = [self rowsInRect:clipRect];

NSUInteger selectedRow = [selectedRowIndexes firstIndex];
while (selectedRow != NSNotFound)
{
if (selectedRow == -1 || !NSLocationInRange(selectedRow, visibleRows))
{
selectedRow = [selectedRowIndexes indexGreaterThanIndex:selectedRow];
continue;
}

// determine if this is a group row or not
id delegate = [self delegate];
BOOL isGroupRow = NO;
if ([delegate respondsToSelector:@selector(outlineView:isGroupItem:)])
{
id item = [self itemAtRow:selectedRow];
isGroupRow = [delegate outlineView:self isGroupItem:item];
}

if (isGroupRow)
{
[[NSColor alternateSelectedControlColor] set];
} else {
[[NSColor secondarySelectedControlColor] set];
}

NSRectFill([self rectOfRow:selectedRow]);
selectedRow = [selectedRowIndexes indexGreaterThanIndex:selectedRow];
}
}

关于objective-c - NSOutlineView 更改组行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5043929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com