-->

NSTableView - select row and respond to mouse even

2019-01-24 23:29发布

问题:

This question already has an answer here:

  • Respond to mouse events in text field in view-based table view 5 answers

I have a view based NSTableView in which the cells contain a number of controls including text fields and edit fields. When a user tries to click on a control within a cell in order to, for example, start editing a text field, the click's main objective is ignored and the cell gets selected. Another click is then needed to perform the action originally intended, and even this click is subject to a delay before it's taken into account.

How can I avoid this problem and have the row selected and the mouse event forwarded to the control in one go?

回答1:

I solved this issue by subclassing NSTableView:

@implementation QuickResponseTableView

- (BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event
{
    // This allows the user to click on controls within a cell withough first having to select the cell row
    return YES;
}

@end


回答2:

Had the same problem. After much struggle, it magically worked when I selected None as against the default Regular (other option is Source List) for the Highlight option in IB! The accepted answer appears to be more specific but a little hacky compared to this.