NSCollectionView
内の項目を、ドラッグアンドドロップの代行メソッドを使用して移動したいとします。私はアイテムが落ちるまで作業します。目的地インジケータ(ギャップ)はありません。マウスを離すと、アイテムが跳ね返っています。代理人validateDrop
とacceptDrop
は決して呼び出されません。 CollectionViewItems
は、カスタムオブジェクトからのデータを示している。NSCollectionView内のドラッグアンドドロップの例
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
@Willekeは応答に感謝しますが、どういう意味ですか?どのパスがどこに欠けていますか?ありがとう! – JFS
'validateDrop'と' acceptDrop'関数に正しいパラメータがありません。新しいバージョンには 'proposedIndexPath'と' indexPath'パラメータがあります。 – Willeke