【问题标题】:NSCollectionView `validateDrop` delegate not getting calledNSCollectionView`validateDrop`委托没有被调用
【发布时间】:2017-02-18 22:19:11
【问题描述】:

我有一个简单的 1 部分 NSCollectionView 并且对不让拖放工作感到疯狂。集合视图具有自定义集合视图项,其中包含显示自定义对象数据的标签。一切正常,直到拖动的项目应该放在新位置之间。但是当我拖动项目时,放置目的地没有放置指示器(通常是间隙)。我发现下面的NSCollectionView 委托没有被调用。

func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo,
                   proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>,
                   dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation {

    print("validateDrop", proposedDropIndexPath) // never shows up in the console

    if proposedDropOperation.pointee == NSCollectionViewDropOperation.on {
        proposedDropOperation.pointee = NSCollectionViewDropOperation.before
    }
        print("validateDrop", proposedDropIndexPath)
        return NSDragOperation.move
    }

我错过了什么??? 谢谢!

编辑 1

我只想使用拖放来重新排序集合视图中的项目。它不打算在集合视图中拖动外部内容。

【问题讨论】:

标签: swift drag-and-drop nscollectionview


【解决方案1】:

你应该注册合适的拖拽类型,匹配collectionView(_:, pasteboardWriterForItemAt:)的返回

例如,如果我们想在粘贴板上添加一个 NSImage,我们应该为集合视图注册 tiff 类型。

self.collectionView.registerForDraggedTypes([.tiff])
func collectionView(_ collectionView: NSCollectionView,
                         indexPath: IndexPath) -> NSPasteboardWriting? {
        SnsDebug("collectionView pasteboardWriterForItemAt")
        let image = self.viewModel.images.value[indexPath.item]
        return image as NSPasteboardWriting? // NSImage
    }

【讨论】:

    【解决方案2】:

    你是否为拖拽类型注册了collectionview?

      collectionView.registerForDraggedTypes([NSPasteboard.PasteboardType])
    

    【讨论】:

      猜你喜欢
      • 2011-08-15
      • 2022-01-22
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      相关资源
      最近更新 更多