如何允许将行拖到NSOutlineView
之外并拖到另一个应用程序(例如finder,safari等)中?
我尝试实现canDragRows:rowIndexes:mouseDownPoint
(总是返回true
),但仍然无法移动行。
这是我正在使用的代码:
class OutlineView: NSOutlineView {
var desinationDelegate: DestinationViewDelegate?
override func awakeFromNib() {
register(forDraggedTypes: [NSFilenamesPboardType])
setDraggingSourceOperationMask(.every, forLocal: false)
verticalMotionCanBeginDrag = true
draggingDestinationFeedbackStyle = .regular
}
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
return NSDragOperation()
}
override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
return true
}
override func draggingEnded(_ sender: NSDraggingInfo?) {
guard
window?.isKeyWindow ?? false,
let pasteBoard = sender?.draggingPasteboard(),
let urls = pasteBoard.readObjects(forClasses: [NSURL.self], options: nil) as? [URL],
urls.count > 0 else {
return
}
desinationDelegate?.receive(urls: urls)
}
override func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {
return NSDragOperation()
}
override func pasteboard(_ sender: NSPasteboard, provideDataForType type: String) {
sender.setData(desinationDelegate?.item(atRow: selectedRow), forType: NSFilenamesPboardType)
}
override func canDragRows(with rowIndexes: IndexSet, at mouseDownPoint: NSPoint) -> Bool {
return true
}
}
我添加了
func outlineView(_ outlineView: NSOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
pasteboard.setString("Foo", forType: NSFilenamesPboardType)
return false
}
进入我的ViewController
类(即NSOutlineViewDataSource
)。 setString
(或setObject
)会产生错误。 Stacktrace:
2019-03-05 15:48:45.659838-0800 Reader[97347:788552] -[NSTaggedPointerString count]: unrecognized selector sent to instance 0x5f49856bc5031b0d
2019-03-05 15:48:51.396627-0800 Reader[97347:788552] [General] -[NSTaggedPointerString count]: unrecognized selector sent to instance 0x5f49856bc5031b0d
2019-03-05 15:48:51.411676-0800 Reader[97347:788552] [General] (
0 CoreFoundation 0x00007fff48e73e65 __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff74ecf720 objc_exception_throw + 48
2 CoreFoundation 0x00007fff48ef122d -[NSObject(NSObject) __retain_OA] + 0
3 CoreFoundation 0x00007fff48e15820 ___forwarding___ + 1486
4 CoreFoundation 0x00007fff48e151c8 _CF_forwarding_prep_0 + 120
5 AppKit 0x00007fff46a57680 +[_NSFilenamesPboardTypeConverter _setConvertedData:pboard:generation:inItem:] + 99
6 AppKit 0x00007fff46698565 -[NSPasteboard _setData:forType:index:usesPboardTypes:] + 648
7 Reader 0x0000000100011752 $S6Reader14ViewControllerC07outlineB0_10writeItems2toSbSo09NSOutlineB0C_SayypGSo12NSPasteboardCtF + 210
8 Reader 0x0000000100011810 $S6Reader14ViewControllerC07outlineB0_10writeItems2toSbSo09NSOutlineB0C_SayypGSo12NSPasteboardCtFTo + 128
9 AppKit 0x00007fff46b520f6 -[NSTableView _performClassicDragOfIndexes:hitRow:event:] + 278
10 AppKit 0x00007fff466c9378 -[NSTableView _performDragFromMouseDown:] + 474
11 AppKit 0x00007fff466c767c -[NSTableView mouseDown:] + 798
12 AppKit 0x00007fff466c7148 -[NSOutlineView mouseDown:] + 73
13 AppKit 0x00007fff4654bd63 -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5668
14 AppKit 0x00007fff4647fd8b -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2319
15 AppKit 0x00007fff4647f231 -[NSWindow(NSEventRouting) sendEvent:] + 481
16 AppKit 0x00007fff4631c4a8 -[NSApplication(NSEvent) sendEvent:] + 336
17 AppKit 0x00007fff46309cf1 -[NSApplication run] + 755
18 AppKit 0x00007fff462f93f7 NSApplicationMain + 780
19 Reader 0x0000000100014ead main + 13
20 libdyld.dylib 0x00007fff75f9e08d start + 1
)
我可以拖动这些单元格,但是除了它们似乎没有其他东西(换句话说,我不能将它们放到任何地方)。
在writeItems:toPasteboard
中,我有以下代码:
pasteboard.declareTypes([NSURLPboardType], owner: nil)
pasteboard.setData(item.url.dataRepresentation, forType: NSURLPboardType)