Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在选择NSOutlineView行时设置背景色_Objective C_Macos_Cocoa_Nsoutlineview_Nsbuttoncell - Fatal编程技术网

Objective c 如何在选择NSOutlineView行时设置背景色

Objective c 如何在选择NSOutlineView行时设置背景色,objective-c,macos,cocoa,nsoutlineview,nsbuttoncell,Objective C,Macos,Cocoa,Nsoutlineview,Nsbuttoncell,我有一个大问题,无法解决 我有一个NSOutlineView,其中包含一个NSButtonCell,它绑定到arrayController,从那里开始设置它的标题,并根据某个值在单元格背景中通过代码设置图像 使用这些代码设置图像 [cell setImage:[NSImage imageNamed:@"sbWarn.png"]]; [cell setImagePosition:NSImageOverlaps]; [cell setBackgroundColor:[NSColor clearCol

我有一个大问题,无法解决

我有一个
NSOutlineView
,其中包含一个
NSButtonCell
,它绑定到
arrayController
,从那里开始设置它的
标题
,并根据某个值在单元格背景中通过代码设置图像

使用这些代码设置图像

[cell setImage:[NSImage imageNamed:@"sbWarn.png"]];
[cell setImagePosition:NSImageOverlaps];
[cell setBackgroundColor:[NSColor clearColor]];
问题是,当我选择一行时,所选行显示黑色背景。我尝试了各种可能的方法,比如将背景设置为清晰的颜色等等


我怎样才能让它看起来好看

这种方法应该有效。它来自内存,因此无法保证,但快速模型似乎给出了您想要的结果:

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

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

您可能只能通过创建自己的单元子体并在那里绘制来解决此问题。这也打开了更多的可能性,你如何绘制那里的内容(例如,不止一个图像,徽章,不同的文本颜色等)。我认为有问题的大纲视图是基于单元格的。基于视图的表视图(因此也包括大纲视图)提供了绘制背景的方法(尽管您必须在文档中仔细研究),但是您将失去以相同方式使用绑定的能力,因此不确定是否值得。这里可能有一些东西:@Monolo:是的,它是基于单元格的。我尝试了各种方法,无边界、设置透明、不透明,以及nsbuttoncell的各种可用位置。如果在窗口上画了一个按钮,它就工作了,在nsview中就是这样,所以这增加了复杂性。@Monolo:看到答案,我想投反对票,但我很少这样做:p@AnoopVaidya我只是在我的小测试床上测试了它,它也能工作。您是否在
大纲中执行任何操作查看:willDisplayCell:forTableColumn:item:
?如果我直接在nswindow中执行,它会工作,但会在NSView上绘制。我不确定我是否理解其中的区别,但祝你好运,我正在追踪这一点:-)现在我正在尝试子类化
NSButtonCell
。。。你知道吗,这是我以前从未做过的不,不是特别的@如果您不想在
outlineView:willDisplayCell:forTableColumn:item:
中设置简单的颜色,MikeLischke的评论可能是正确的。