我想使用拖放委託方法在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'函數沒有正確的參數。新版本有一個'suggestedIndexPath'和'indexPath'參數。 – Willeke