How to set background color while selecting row of NSOutlineView

假如想象 提交于 2019-12-10 19:28:35

问题


I am having a big problem and not able to solve it.

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.

The Image is set using these codes

[cell setImage:[NSImage imageNamed:@"sbWarn.png"]];
[cell setImagePosition:NSImageOverlaps];
[cell setBackgroundColor:[NSColor 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.


回答1:


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.

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



回答2:


I think you have to use button type as "Momentary Change". You can change it from button properties.



来源:https://stackoverflow.com/questions/16012187/how-to-set-background-color-while-selecting-row-of-nsoutlineview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!