1

我注意到我的应用程序中有奇怪的行为,但找不到解决方法:我有NSOutlineView和自定义NSTableRowView并支持拖放。

当我首先在大纲视图上拖动项目(例如文件)时,它会绘制正确的突出显示(我已经覆盖drawDraggingDestinationFeedbackInRect),但是在第二次拖动时,我有这个蓝色突出显示,如果我调试视图层次结构,我可以看到NSView被添加到行.

我附上了一个小动画,它准确地显示了发生了什么(我还添加了具有相同NSTableRowView的NSTableView):

顶部:NSOutlineView

底部:NSTableView

在此处输入图像描述

视图层次结构(对于 NSOutlineView):

在此处输入图像描述

这是我的NSTableRowView代码

import Cocoa

class TestRowView: NSTableRowView {

    override func drawSelectionInRect(dirtyRect: NSRect) {
        NSColor.yellowColor().setFill()
        NSBezierPath(rect: self.bounds).fill()
    }

    override func drawDraggingDestinationFeedbackInRect(dirtyRect: NSRect) {
        NSColor.greenColor().setFill()
        NSBezierPath(rect: self.bounds).fill()
    }

}

以下是NSOutlineView的数据源和委托方法:

func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {
    return item == nil ? allItems.count : 0
}

func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
    return false
}

func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
    return allItems[index]
}

func outlineView(outlineView: NSOutlineView, viewForTableColumn tableColumn: NSTableColumn?, item: AnyObject) -> NSView? {
    return outlineView.makeViewWithIdentifier("DataCell", owner: self)
}

func outlineView(outlineView: NSOutlineView, rowViewForItem item: AnyObject) -> NSTableRowView? {
    return TestRowView()
}

func outlineView(outlineView: NSOutlineView, heightOfRowByItem item: AnyObject) -> CGFloat {
    return 60.0
}

func outlineView(outlineView: NSOutlineView, objectValueForTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) -> AnyObject? {
    return item
}

func outlineView(outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: AnyObject?, childIndex index: Int) -> Bool {
    return true
}

func outlineView(outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: AnyObject?, proposedChildIndex index: Int) -> NSDragOperation {
    return .Copy
}

我试过的:

  1. 继承 NSOutlineView 并覆盖 AcceptFirstResponder

  2. 覆盖背景样式

  3. 尝试了不同的 selectionHighlightStyle 的
  4. 使用 NSTableViewDraggingDestinationFeedbackStyleNone
4

1 回答 1

0

我找到了一种解决我的问题的方法,这就是我所做的:

第一件事 - 子类NSOutlineView具有以下属性被覆盖:

override var acceptsFirstResponder: Bool{
    get{
        return false
    }
}

其次,在我的NSTableRowView子类中,我添加了以下内容:

override var selectionHighlightStyle: NSTableViewSelectionHighlightStyle {
    get{
        return self.emphasized ? .None:.Regular
    }
    set{

    }
}

我不能说这是一个好的解决方案,甚至是一个完整的解决方案,但总比没有好。

有时我仍然得到灰色背景,所以我还在我的NSTableCellViewoverride var backgroundStyle : NSBackgroundStyle子类中的属性的设置器中添加了一个检查。

于 2016-07-02T20:37:55.487 回答