2013-04-15 68 views
3

我有一个大问题,无法解决它。如何设置背景颜色,同时选择NSOutlineView行

我有一个NSOutlineView包含其从那里它被设置其Title势必arrayController和基于某些值的图像在由代码单元格的背景设置NSButtonCell

该图像是使用这些代码

[cell setImage:[NSImage imageNamed:@"sbWarn.png"]]; 
[cell setImagePosition:NSImageOverlaps]; 
[cell setBackgroundColor:[NSColor clearColor]]; 

问题是,当我选择一行所选择的行示出了在黑色背景。我试着像设置背景clearColor等

我怎样才能使它看起来好每一个可能的方式。

+1

你或许可以通过创建自己的细胞后裔并在那里做绘图来解决这个问题。这样,您可以更容易地绘制如何在其中绘制内容(例如,多个图像,徽章,不同的文本颜色等)。 –

+0

我认为所讨论的大纲视图是基于单元格的。基于查看的表格视图(因此也提供了大纲视图)提供了绘制背景的方法(尽管您需要在文档中进行挖掘),但是您将失去以相同方式使用绑定的能力,所以不知道它是否值得。 – Monolo

+0

可能有东西在这里,虽然:http://stackoverflow.com/questions/2062605/how-to-set-background-color-of-cell-with-nsbuttoncell-type-in​​-nstableview – Monolo

回答

1

这种方法应该可行。这是从内存中,所以没有保证,但是快速的样机似乎给您正在寻找的结果:

// 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]; 
} 

在你的情况下,你当然只适用于与钮扣电池列单元格的背景颜色。

这是所使用的钮扣电池,在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]; 
+0

buttonCell已经在UI中创建。 –

+0

@AnoopVaidya我刚刚在我的小测试平台上测试过,它也可以工作。你在'outlineView:willDisplayCell:forTableColumn:item:'中做了什么吗? – Monolo

+0

如果我在nswindow中直接使用它,但它在nsview –

0

我认为你必须使用按钮类型作为“瞬间改变”。你可以从按钮属性改变它。