Search code examples
cocoanstableview

Dont deselect a selected row in view based NStableview when clicked outside


In my view based NSTableView i don't want to deselect the selected cell, when clicking in an empty part of the NSTableView, how to obey this default behavior?


Solution

  • Improved version from Neha's answer (this obeys the select / deselect)

    Subclass NSTableView and implement :

    - (void)mouseDown:(NSEvent *)theEvent {
    
        NSPoint globalLocation = [theEvent locationInWindow];
        NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
        NSInteger clickedRow = [self rowAtPoint:localLocation];
    
        if(clickedRow != -1) {
            [super mouseDown:theEvent];
        }
    }
    

    We just ignore the event, when we don't hit a row...