如何正确设置NSTableHeader的backgroundcolor?

时间:2017-07-26 14:05:39

标签: swift cocoa subclass background-color nstableview

我花了最后一天搜索互联网,了解如何设置NSTableHeaderCell的背景颜色。我所取得的就是:

func tableView(_ numberTableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

        if tableColumn!.identifier == "description" {

            let cell = NSTableHeaderCell()
            cell.title = "Header Title"
            cell.backgroundColor = NSColor.red
            cell.drawsBackground = true
            cell.isBordered = false
            cell.font = NSFont(name: "System", size: 13)
            tableColumn!.headerCell = cell
        }
        return nil
    }

结果如下所示:

enter image description here

我尝试将字体更改为NSFont(name: "Helvetica", size: 26),然后在启动应用程序后看起来像:

enter image description here

但是第一次点击单元格后,单元格再次弹出:

enter image description here

我想知道是否有办法设置整个单元格的背景颜色,而不是在点击后变小。我也尝试将NSTableHeaderView子类化,但这只是绘制了整个HeaderView,并且不能再设置标题。获得一些想法将是一个很大的帮助。

编辑:

根据建议的答案,我尝试将NSTableHeaderCell子类化并设置:tableColumn?.headerCell = MyHeaderCell()

class MyHeaderCell: NSTableHeaderCell {

    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
        super.draw(withFrame: cellFrame, in: controlView)
        NSColor.red.set()
        self.backgroundColor = NSColor.red
        NSRectFillUsingOperation(cellFrame, .sourceOver)

    }
}

这就是结果:

enter image description here

所以现在整个HeaderView都是彩色的。通过单击一个单元格,单元格再次变为白色。不知怎的,我仍然没有弄清楚如何改变细胞的真实背景。

0 个答案:

没有答案