简体   繁体   English

Swift:在NSOutlineView中拖放

[英]Swift: Drag & Drop in NSOutlineView

I'm building an NSOutlineView using Swift. 我正在使用Swift构建NSOutlineView。 I have the view in place and now I'm starting on drag and drop to move items around in the view. 我有适当的视图,现在开始拖放操作以在视图中移动项目。 I'm using This as my starting point. 我以This为起点。 I have drag working, click and drag around the outline, but I can't make the drop part work; 我已经进行了拖动,单击并在轮廓上拖动,但是无法使放置部分起作用; I'm not seeing the horizontal marker and I get nothing but a beep when I drop. 我没看到水平标记,掉落时只听到一声哔哔声。 I'm missing something obvious but I can't for the life of me see it. 我缺少明显的东西,但我一生都看不到。

This is my d&d code from my NSOutlineViewDelegate: 这是我的NSOutlineViewDelegate中的d&d代码:

func outlineView(_ outlineView: NSOutlineView, pasteboardWriterForItem item: Any) -> NSPasteboardWriting? {

    let pp = NSPasteboardItem()

    // working as expected here
    if let fi = item as? FileItem {
        pp.setString( fi.Path, forType: "public.text" )

        print( "pb write \(fi.Name)")

    } else {
        print( "pb write, not a file item \(item)")
    }

    return pp
}

func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {

    print( "validate: \(item)")
    return NSDragOperation.move
    //
}

func outlineView(_ outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {

    print( "accept drop: \(item)")
    return true
}

And I've registered for drag types in ViewDidLoad(): 我已经在ViewDidLoad()中注册了拖动类型:

OutlineViewOutlet.register( forDraggedTypes: ["public.txt"] )

Any thoughts appreciated, thanks. 任何想法表示赞赏,谢谢。

Answering my own question; 回答我自己的问题; wrong type in register(). register()中的类型错误。 This works: 这有效:

OutlineViewOutlet.register( forDraggedTypes: ["public.data"] )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM