swift - NSCollectionView `validateDrop` 委托(delegate)未被调用

标签 swift drag-and-drop nscollectionview

我有一个简单的 1 部分 NSCollectionView 并且因为没有让拖放工作而变得疯狂。 Collection View 具有自定义 Collection View 项,标签显示自定义对象的数据。一切正常,直到拖动的项目应该在新位置之间掉落为止。但是当我拖动项目时,在放置目的地没有放置指示器(通常是一个间隙)。我发现未调用以下 NSCollectionView 委托(delegate)。

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

我只想使用拖放来重新排序 Collection View 中的项目。它不是为了在 Collection View 中拖动外部内容。

最佳答案

您应该注册适当的拖动类型,该类型匹配 collectionView(_:, pasteboardWriterForItemAt:) 的返回值

例如,如果我们想将 NSImage 添加到粘贴板,我们应该为 Collection View 注册 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
    }

关于swift - NSCollectionView `validateDrop` 委托(delegate)未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321333/

相关文章:

ios - CAGradientLayer 在 4 个不同的角上有 4 种颜色

Swift:无法为 UINavigationController 工具栏添加工具栏按钮

ios - 无法在 cocoa touch uiview 类和 Storyboard 中的 subview 之间控制拖动 xcode

arrays - 在单个循环中更新多个累加器时遇到问题

javascript - 如何用JS制作可移动的表单?

jQuery UI - 如何知道拖动的元素是否位于元素 #x 的顶部?

drag-and-drop - 使用 Raphael JS 进行拖放和形状旋转

xcode - 数据绑定(bind)/NSArrayController : What is the job of the Object Controller

objective-c - NSCollectionView的visibleRect属性

macos - 具有自定义 View 的 NSCollectionViewItem