NSCollectionView 中的拖放示例 [英] Example for Drag and Drop inside NSCollectionView

查看:44
本文介绍了NSCollectionView 中的拖放示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用拖放委托方法在 NSCollectionView 中移动项目.我让它工作,直到该项目被丢弃.没有目标指示器(间隙),当释放鼠标时,项目会弹回.代表 validateDropacceptDrop 永远不会被调用.CollectionViewItems 显示来自自定义对象的数据:

I would like to move items inside a NSCollectionView using its drag and drop delegate methods. I get it working until the item should get dropped. There is no destination indicator (gap) and when releasing the mouse the item is bouncing back. The delegates validateDrop and acceptDrop never gets called. The CollectionViewItems are showing data from custom objects:

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
}

这到底是什么...我认为将要拖动到粘贴板中的项目数据写入有问题.任何建议.

What the heck... I think there is something wrong with writing the item-data to be dragged in the pasteboard. Any suggestions.

推荐答案

这个问题有两个可能的原因.在这种特殊情况下,Willeke 的回答是正确的.实际上,您仍然可以使用所有拖放委托函数的旧 indexSet 版本,但如果您这样做,则必须确保 所有这些 都是旧版本,不要将旧版本和新的!

There are two possible causes of this issue. In this specific case Willeke's response is correct. You can in fact still use the old indexSet versions of all the drag-and-drop delegate functions, but if you do then you must make sure that all of them are the old versions, no mixing old and new!

简而言之,如果您的 canDragItems: 委托函数有一个名为 indexPaths 的参数,那么请确保 writeItemsAt:validateDrop:>acceptDrop: 函数也使用 IndexPaths,而不是 IndexSets.

In short, if your canDragItems: delegate function has a parameter called indexPaths, then ensure that writeItemsAt:, validateDrop: and acceptDrop: functions also make use of IndexPaths, not IndexSets.

其次,在我的例子中就是这种情况,检查您是否记得注册您希望集合视图响应的拖动类型,即 viewDidLoad - 请参阅代码片段下面 - 当然,在首先生成拖动数据时,在粘贴板中设置了相同的拖动类型!

Secondarily, and this was the case in my instance, check that you have remembered to register for the drag types you want your collection view to respond to somewhere sensible, i.e. viewDidLoad - see the code snippet below - and of course that the same drag type is being set in the pasteboard when generating the drag data in the first place!

collectionView.register(forDraggedTypes: [dragType])

这篇关于NSCollectionView 中的拖放示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆