NSCollectionViewItem双击动作?

问题描述 投票:5回答:2

如何为用户双击NSCollectionViewItem时设置操作。例如,NSTableViewsetDoubleAction方法。 NSCollectionView有类似的东西吗?

谢谢

objective-c cocoa nscollectionview nscollectionviewitem
2个回答
2
投票

你可能想在你的NSCollectionViewItem中处理这个问题,而不是NSCollectionView本身(为了解决你的NSTableView类比)。


2
投票

我知道这个问题很古老,但它现在成为谷歌的第三个结果,而且我发现了一种我在其他地方没有见过的另一种非常直接的方法。 (我不仅需要操纵所表示的项目,而且还需要在我的应用程序中执行更复杂的工作。)

NSCollectionView继承自NSView,因此您可以简单地创建自定义子类并覆盖mouseDown。这并非完全没有缺陷 - 在使用NSCollectionViewindexPathForItem方法之前,您需要检查点击次数,并将点从主窗口转换为集合视图的坐标:

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)
...

这感觉好像我终于找到了Apple打算使用的方法 - 否则,indexPathForItem(at:)没有理由存在。

© www.soinside.com 2019 - 2024. All rights reserved.