2017-02-19 4 views
0

NSCollectionView内の項目を、ドラッグアンドドロップの代行メソッドを使用して移動したいとします。私はアイテムが落ちるまで作業します。目的地インジケータ(ギャップ)はありません。マウスを離すと、アイテムが跳ね返っています。代理人validateDropacceptDropは決して呼び出されません。 CollectionViewItemsは、カスタムオブジェクトからのデータを示している。NSCollectionView内のドラッグアンドドロップの例

enter image description here

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 
} 

一体何...私はペーストボードにドラッグする項目データを書き込むことに問題があると思います。助言がありますか。

+0

メソッドの宣言を確認してください。 「パス」がありません。 – Willeke

+0

@Willekeは応答に感謝しますが、どういう意味ですか?どのパスがどこに欠けていますか?ありがとう! – JFS

+0

'validateDrop'と' acceptDrop'関数に正しいパラメータがありません。新しいバージョンには 'proposedIndexPath'と' indexPath'パラメータがあります。 – Willeke

答えて

0

この問題の原因は2つあります。この特定のケースでは、ウィレケの応答は正しい。実際には、すべてのドラッグアンドドロップデリゲート関数の古いindexSetバージョンを使用することはできますが、その場合はのすべてがであることを確認しなければなりません。あなたのcanDragItems:デリゲート関数がindexPathsというパラメータを持っている場合は要するに

、その後writeItemsAt:validateDrop:acceptDrop:機能もIndexPaths、ないIndexSetsを利用することを確認してください。

二次的に、これは私のケースでは、あなたのコレクションビューがどこかに敏感な、つまりviewDidLoadに反応するようにするドラッグタイプを登録することを覚えていることを確認してください。最初にドラッグデータを生成するときに同じドラッグタイプがペーストボードに設定されています。

collectionView.register(forDraggedTypes: [dragType]) 
関連する問題