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

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

问题描述

我想使用其拖放委托方法将项目移动到 NSCollectionView 中。我得到它的工作,直到物品掉落。没有目的地指示符(间隙),当释放鼠标时,项目反弹。代理人 validateDrop acceptDrop 永远不会被调用。 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.

推荐答案

这个问题有两个可能的原因。在这个具体情况下,威廉克的回应是正确的。实际上,您仍然可以使用所有拖放代理功能的旧的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 - 看下面的代码片段,当然,在生成拖动数据时,在粘贴板中设置相同的拖动类型! / p>

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天全站免登陆