如何设置用户双击NSCollectionViewItem时的操作。例如,NSTableView具有setDoubleAction方法。NSCollectionView也有类似的功能吗?
谢谢
发布于 2009-10-12 00:52:16
您可能希望在NSCollectionViewItem中处理此问题,而不是在NSCollectionView本身中处理(以解决您的NSTableView类比问题)。
发布于 2017-07-17 05:31:20
我知道这个问题很古老,但它现在是Google上的第三个结果,我找到了一种不同的、非常简单的方法,我在其他地方没有见过这种方法。(我不仅需要操作表示的项目,还需要在我的应用程序中完成更复杂的工作。)
NSCollectionView继承自NSView,因此您可以简单地创建自定义子类并覆盖mouseDown。这并不是完全没有缺陷-在使用NSCollectionView的indexPathForItem方法之前,您需要检查点击计数,并将点从主窗口转换为集合视图的坐标:
override func mouseDown(with theEvent: NSEvent) {
if theEvent.clickCount == 2 {
let locationInWindow = theEvent.locationInWindow
let locationInView = convert(locationInWindow, from: NSApplication.shared.mainWindow?.contentView)
if let doubleClickedItem = indexPathForItem(at: locationInView){
// handle double click - I've created a DoubleClickDelegate
// (my collectionView's delegate, but you could use notifications as well)
...这感觉好像我终于找到了苹果打算使用的方法--否则,indexPathForItem(at:)就没有存在的理由了。
https://stackoverflow.com/questions/1552220
复制相似问题