我有一个简单的 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/