简体   繁体   中英

Swift: Drag & Drop in NSOutlineView

I'm building an NSOutlineView using Swift. 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. 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:

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():

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

Any thoughts appreciated, thanks.

Answering my own question; wrong type in register(). This works:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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