3

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.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • 1
    You can probably solve this only by creating your own cell descendant and do the drawing there. This opens also more possibilties how you draw the content there (e.g. more than oneimage, badges, different text colors etc. etc.). – Mike Lischke Apr 15 '13 at 11:30
  • I take it that the outline view in question is cell based. View based table views (and hence also outline views) provide the means to draw the backgrounds as like (although you'll have to dig around in the docs), but you'd lose the ability to use bindings in the same way, so not sure it's worth it. – Monolo Apr 15 '13 at 18:52
  • There may be something here, though: http://stackoverflow.com/questions/2062605/how-to-set-background-color-of-cell-with-nsbuttoncell-type-in-nstableview – Monolo Apr 15 '13 at 18:54
  • @Monolo: yes it is cell based. I tried every way, borderless, setTransparent, opaque, and varites of available prpoerties of nsbuttoncell. If a button is drawn on window it works, here it is in nsview, so this adds more complexity. – Anoop Vaidya Apr 15 '13 at 18:55
  • @Monolo: by seeing the answer, i wished to downvote, but I rarely do that :p – Anoop Vaidya Apr 15 '13 at 18:56

2 Answers2

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];
Monolo
  • 18,205
  • 17
  • 69
  • 103
0

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

ADJ
  • 1,531
  • 1
  • 11
  • 15