NSBrowser selection of row and action of NSBrowserCell

232 Views Asked by At

I am having an NSBrowser in one of my windows. It has a checkbox, image and textbox as you see in the screenshot.

I am struggling to do two things:

  1. Change the row selection color. By default it is blue.
  2. Action on the checkbox

The checkbox + image + textbox is added to the subclass of NSBrowserCell like this:

- (instancetype)init
{
    self = [super init];
    if (self) {
        _buttonCell = [[NSButtonCell alloc] init];
        [_buttonCell setButtonType:NSButtonTypeSwitch];
        [_buttonCell setTarget:self];
        [_buttonCell setAction:@selector(cellButtonClick:)];
        [_buttonCell setTitle:@""];

        _imageCell = [[NSImageCell alloc] init];

        _textCell = [[NSTextFieldCell alloc] init];

    }
    return self;
}

I've added target and Action too, but it is not called.

How do I achieve these two things?

Any guidance will be appreciated.

enter image description here

1

There are 1 best solutions below

3
On

Caveat: customizing NSBrowser/NSBrowserCell is usually hard. It's a legacy view class that's been essentially abandoned for years, and it has a lot of undocumented quirks.

Having said that, try overriding -highlightColorWithFrame:inView: to use a different color for highlighting the row. It that's all you need, then I think that should do it.

As for the action, NSCell (unlike views and controls) is not a subclass of NSResponder and doesn't perform any automatic mouse event processing. (Cells are just helper objects to draw the view of a control/element.)

You'll probably have to catch the event at the browser view level, then perform hit testing to find the column/row that contains your check box cell. If you find a hit, then post the action message yourself (-sendAction:to:from:)—which is literally what a NSControl view does.