在我的应用程序中,我实现了将文件拖放到基于 View 的 NSTableview 上的可能性。 NSTableView 使用的 NSTableCellView 包含三个标签(NSTextFieldCell)和一张图像(NSImageCell)。我直接在使用的 NSTableView 子类中定义了允许的 DragType:
required init?(coder: NSCoder) {
super.init(coder: coder)
if #available(OSX 10.13, *) {
registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
} else {
registerForDraggedTypes([kUTTypeFileURL as NSPasteboard.PasteboardType])
}
self.draggingDestinationFeedbackStyle = NSTableView.DraggingDestinationFeedbackStyle.regular
}
deinit {
unregisterDraggedTypes()
}
其余的放置操作是通过 NSTableViewDataSource 和两个函数执行的:
func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool
func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation
到目前为止,除了一件烦人的小事情之外,一切正常:如果我在拖动过程中将鼠标移到相应行中的图像上,则该行的选择将被取消。所以简单地说:我在 TableView 的一行中的拖动过程中移动鼠标指针 -> 根据需要选择该行并保持这一状态...直到我将鼠标移动到图像区域...Swash,选择又消失了。如果我再次将鼠标从图像范围移动到该行的“其余部分”,则该行将再次被选中。
是否图像也需要 DraggedTypes,并且功能也必须实现,或者我是否忽略了某些内容(a'la“嘿,图像,获取对 Superview 的放置请求以继续”:))? NSTableCellView的三个标签显然不需要这个。
我注意到 NSOutlineView 也有类似的行为:只要我没有到达相应条目的图像,拖放就可以正常工作。
最佳答案
感谢Willeke为我指明了正确的方向!在单元格的 init 中使用 image.controlView?.unregisterDraggedTypes()
解决了该问题。
关于swift - 基于 View 的 NSTableView 中的拖放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57256949/