Reputation: 882
I have a NStableView which is NSView based. In each row also have a NSImageView which is subview of NSView. I have subclassed this NSImageView then overrided mouseDown method. The problem is tableViewSelectionDidChange is also getting fired when user clicks on this imageview. I want only mouseDown to be called but not tableViewSelectionDidChange.
If I set selectionHighlightStyle of table to NSTableViewSelectionHighlightStyle.None then only mouseDown of image view is called. And if I don't give selectionHighlightStyle to NSTableViewSelectionHighlightStyle.None then both mouseDown of imageview and tableViewSelectionDidChange are getting called. Setting selectionHighlightStyle to none seems to solve my problem but is this correct approch? Or I am getting this behaviour because of some bug in appkit?
Also I can't find this behaviour documented somewhere.
Upvotes: 0
Views: 1270
Reputation: 2419
I think the correct approach you be to subclass NSTableView and override the NSResponder method -(BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event
You'll have a finer control over which view will get the mouse event, and won't have to resort to "hackery" like changing the table view selection style.
From the apple docs:
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.
Upvotes: 3