gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:13:11 25 4
gpt4 key购买 nike

我有一个基于 NSView 的 NStableView。每行还有一个 NSImageView,它是 NSView 的 subview 。我已将此 NSImageView 子类化,然后重写 mouseDown 方法。问题是当用户单击此 ImageView 时,tableViewSelectionDidChange也会被触发。我只想调用 mouseDown 而不是 tableViewSelectionDidChange。

如果我将表的selectionHighlightStyle设置为NSTableViewSelectionHighlightStyle.None,则仅调用 ImageView 的mouseDown。如果我不将 selectionHighlightStyle 赋予 NSTableViewSelectionHighlightStyle.None ,那么 imageview 的 mouseDowntableViewSelectionDidChange 都会被调用。将 selectionHighlightStyle 设置为 none 似乎可以解决我的问题,但这是正确的方法吗?或者我之所以出现这种行为是因为 appkit 中的某些错误?

此外,我无法在某处找到此行为的记录。

最佳答案

我认为正确的方法是继承 NSTableView 并重写 NSResponder 方法 -(BOOL)validateProposeFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event

您将可以更好地控制哪个 View 将获得鼠标事件,而不必诉诸“黑客”,例如更改 TableView 选择样式。

来自苹果文档:

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

关于swift - NSTableView 行内鼠标按下的正确行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com