在 NSTableView 的行内鼠标按下的正确行为?

Correct behaviour of Mouse down inside row of NSTableView?

我有一个基于 NSView 的 NStableView。在每一行中还有一个 NSImageView,它是 NSView 的子视图。我已经将这个 NSImageView 子类化,然后覆盖了 mouseDown 方法。问题是 tableViewSelectionDidChange 当用户点击这个图像视图时也会被触发。我只想调用 mouseDown 而不是 tableViewSelectionDidChange.

如果我将 table 的 selectionHighlightStyle 设置为 NSTableViewSelectionHighlightStyle.None 那么只有 mouseDown 的图像视图被调用。如果我不给 selectionHighlightStyleNSTableViewSelectionHighlightStyle.None 然后 mouseDowntableViewSelectionDidChange 正在调用。将 selectionHighlightStyle 设置为 none 似乎可以解决我的问题,但这是正确的方法吗?还是由于 appkit 中的某些错误导致我出现此行为?

我也找不到在某处记录此行为。

我认为正确的做法是继承 NSTableView 并覆盖 NSResponder 方法 -(BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event

您可以更好地控制哪个视图将获得鼠标事件,而不必像更改 table 视图选择样式那样求助于“hackery”。

来自苹果文档:

Specifying How Subviews Should Respond to Events

Views or controls in a table sometimes need to respond to incoming events. To determine whether a particular subview should receive the current mouse event, a table view calls validateProposedFirstResponder:forEvent: in its implementation of hitTest. If you create a table view subclass, you can override validateProposedFirstResponder:forEvent: to specify which views can become the first responder. In this way, you receive mouse events.

The default NSTableView implementation of validateProposedFirstResponder:forEvent: uses the following logic:

Return YES for all proposed first responder views unless they are instances or subclasses of NSControl. Determine whether the proposed first responder is an NSControl instance or subclass.

If the control is an NSButton object, return YES.

If the control is not an NSButton, call the control’s hitTestForEvent:inRect:ofView: to see whether the hit area is trackable (that is, NSCellHitTrackableArea) or is an editable text area (that is, NSCellHitEditableTextArea), and return the appropriate value. Note that if a text area is hit, NSTableView also delays the first responder action.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TableView/RowSelection/RowSelection.html#//apple_ref/doc/uid/10000026i-CH6-SW9