【发布时间】:2019-01-08 07:54:13
【问题描述】:
我正在尝试实现从 NSCollectionView 拖动项目(不仅仅是在其上拖放东西)。
在我的示例代码中,我正在通过拖动注册 CollectionView:
collectionView.registerForDraggedTypes([.URL])
collectionView.setDraggingSourceOperationMask(.every, forLocal: false)
collectionView.setDraggingSourceOperationMask(.every, forLocal: true)
然后,我从 NSCollectionViewDelegate 协议中实现了这些方法:
func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
return true
}
func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
return URL(fileURLWithPath: #file) as NSPasteboardWriting
}
func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) { }
func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) { }
但是他们都没有被调用过! 为什么不呢?
如果我添加这两种方法:
func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
return .move
}
func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
return true
}
然后我可以成功地将文件从桌面拖放到集合视图中,但反过来还是不行。
发生了什么事?
最好的问候,V。
【问题讨论】:
-
收藏视图的“可选择”是否开启?
-
@Willeke:我刚刚再次尝试将
isSelectable设置为false和true,它似乎根本没有任何区别。 :(
标签: macos drag-and-drop delegates nscollectionview nscollectionviewitem