首页 > 解决方案 > 在基于视图的 NSTableView 中拖放问题

问题描述

在我的应用程序中,我实现了将文件拖放到基于视图的 NSTableview 上的可能性。NSTableView 使用的 NSTableCellView 包含三个标签(NSTextFieldCell)和一个图像(NSImageCell)。我已经在使用的 NSTableView 的子类中直接定义了允许的 DragTypes:

    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 有类似的行为:只要我没有看到相应条目的图像,Drag'n'Drop 就可以正常工作。

标签: swiftnstableviewnstablecellview

解决方案


感谢Willeke为我指明了正确的方向!image.controlView?.unregisterDraggedTypes()使用单元格的 init解决了该问题。


推荐阅读