如何在选择NSOutlineView的行时设置背景颜色 [英] How to set background color while selecting row of NSOutlineView
问题描述
我有一个大问题,无法解决它。
I am having a big problem and not able to solve it.
我有一个 NSOutlineView
一个 NSButtonCell
它绑定到 arrayController
从它设置标题
并根据某些值,通过代码在单元格背景中设置图像。
I have an NSOutlineView
which contains a NSButtonCell
which is bound to arrayController
from there it is setting its Title
and based on some value an Image is set in the cell background by codes.
使用这些代码设置图像
[cell setImage:[NSImage imageNamed:@"sbWarn.png"]];
[cell setImagePosition:NSImageOverlaps];
[cell setBackgroundColor:[NSColor clearColor]];
问题是当我选择一行时,所选行显示黑色背景。我尝试了所有可能的方式,如设置背景为clearColor等。
The problem is when I select a row the selected row shows a black background. I tried every possible ways like setting background as clearColor etc.
如何让它看起来不错。
How can I make it look good.
推荐答案
这种方法应该可行。它是从内存,所以没有保证,但一个快速模型似乎给出你正在寻找的结果:
This approach should work. It's from memory, so no guarantees, but a quick mockup seemed to give the results you are looking for:
// Outline view delegate
-(void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSUInteger rowNo = [outlineView rowForItem:item];
NSColor *backgroundColor;
if ( [[outlineView selectedRowIndexes] containsIndex:rowNo] ) {
backgroundColor = // Highlighted color;
}
else {
backgroundColor = // Normal background color;
}
[cell setBackgroundColor: backgroundColor];
}
在你的情况下,你当然只会应用单元格背景颜色
In your case you'll of course only apply the cell background color on the columns with button cells.
这是在 outlineView:dataCellForTableColumn:item:
中创建的按钮单元格。 / p>
This is the button cell used, created in outlineView:dataCellForTableColumn:item:
.
NSButtonCell *cell = [[NSButtonCell alloc] init];
[cell setBezelStyle:NSRegularSquareBezelStyle];
[cell setButtonType:NSToggleButton];
[cell setBordered:NO];
[cell setTitle:@""];
[cell setImage:[NSImage imageNamed:@"green.png"]];
[cell setAlternateImage:[NSImage imageNamed:@"red.png"]];
[cell setImagePosition:NSImageOverlaps];
这篇关于如何在选择NSOutlineView的行时设置背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!