Michael
Michael

Reputation: 2288

How do I deselect all table rows in NSOutlineView when clicking in the empty space of the view?

For example when I click on the red dot below:

before

I want the following deselection to occur:

enter image description here

I set up the view-based NSOutlineView using bindings for both the data source and the selection indexes. So far i've tried to override the TableCellView becomeFirstResponder and also override NSOutlineView's becomeFirstResponder however it seems NSOutlineView never actually gives up first responder status?

Some advice would be very much appreciated!

Upvotes: 3

Views: 2013

Answers (3)

Nikolay
Nikolay

Reputation: 145

Swift 5. in NSOutlineViewDelegate

    func outlineViewSelectionDidChange(_ notification: Notification) {
        //1
        guard let outlineView = notification.object as? NSOutlineView else {
            return
        }
        outlineView.deselectAll(nil)
}

Upvotes: 0

Parag Bafna
Parag Bafna

Reputation: 22930

Use setAction: method of NSOutlineView.

[mOutlineView setAction:@selector(doClick:)];
[mOutlineView setTarget:self];

-(IBAction) doClick:(id)sender;
{
    if ([mOutlineView clickedRow] == -1) {
        [mOutlineView deselectAll:nil];
    }
}

Upvotes: 1

Roger
Roger

Reputation: 4797

I found this post on the topic. The solution appears to be in creating a subclass of NSOutlineView and overriding mouseDown: so that you can determine whether the click was on a row or not. When the click is on a row you just dispatch to super. If it's not you send deselectAll: to your NSOutlineView.

I haven't tried it myself but there are various posts around which come up with comparable code.

Upvotes: 4

Related Questions