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.

有帮助吗?

解决方案

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];

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top