Domanda

By default, NSTableView allows the user to clear the rows selection by clicking anywhere in the blank area of the table view. This however is not always intuitive and sometimes isn’t even possible (for example, when the table view doesn’t actually have any empty area inside itself).

So how do you allow the user to deselect the row by simply clicking on it again? No regular delegate methods (like -tableView:shouldSelectRow:) are called in this case so you can’t capture the click on a row that is already selected, this way.

È stato utile?

Soluzione

You want to define your own subclass of NSTableView and set up -mouseDown: like so:

- (void)mouseDown:(NSEvent *)theEvent {

    NSPoint globalLocation = [theEvent locationInWindow];
    NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
    NSInteger clickedRow = [self rowAtPoint:localLocation];

    BOOL wasPreselected = (self.selectedRow == clickedRow);

    [super mouseDown:theEvent];

    if (wasPreselected)
        [self deselectRow:self.selectedRow];

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top