【发布时间】:2017-02-19 15:58:21
【问题描述】:
我想使用它的拖放委托方法在NSCollectionView 中移动项目。我让它工作,直到该项目被丢弃。没有目标指示器(间隙),当释放鼠标时,项目会弹回。代表validateDrop 和acceptDrop 永远不会被调用。 CollectionViewItems 显示来自自定义对象的数据:
func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
print("canDragItem", indexPaths)
return true
}
func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
let indexData = Data(NSKeyedArchiver.archivedData(withRootObject: indexPaths))
pasteboard.declareTypes(["my_drag_type_id"], owner: self)
pasteboard.setData(indexData, forType: "my_drag_type_id")
print("write data", indexData)
return true
}
func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndex proposedDropIndex: UnsafeMutablePointer<Int>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation {
print("validation")
return NSDragOperation.move
}
func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, index: Int, dropOperation: NSCollectionViewDropOperation) -> Bool {
let pb = NSPasteboard()
let indexData = (pb.data(forType: "my_drag_type_id"))! as Data
let indexPath = (NSKeyedUnarchiver.unarchiveObject(with: indexData)) as! IndexPath
let draggedCell = indexPath.item as Int
print("acceptDrop", draggedCell)
return true
}
这到底是怎么回事...我认为在粘贴板中写入要拖动的项目数据有问题。任何建议。
【问题讨论】:
-
检查方法的声明。缺少“路径”。
-
@Willeke 感谢您的回复,但您是什么意思?哪里缺少什么路径?谢谢!
-
您的
validateDrop和acceptDrop函数没有正确的参数。新版本有一个proposedIndexPath和indexPath参数。 -
嗯,很有趣。我采用了 XCode 提示时建议的方法。我会检查一下。
-
@Willeke 就我而言,这就是问题的原因。 Xcode 自动完成仍然建议旧版本,并且它仍然存在于 NSCollectionView.h 中,但没有提及它不再工作。您能否将其作为答案提交,以便将问题标记为已回答?
标签: swift drag-and-drop nscollectionview