如何在不隐藏列标题的情况下更改 NSTableHeaderView 的背景颜色?

问题描述 投票:0回答:2

我的 Swift OS X 应用程序上有基于视图的 NSTableView,使用有 12 列的故事板。

我对标题视图进行了子类化,并尝试使用不同的方法来更改背景颜色,但它们都隐藏了我的标题。

这是尝试做的许多示例之一:

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)

    // Drawing code here.
    var myLayer = CALayer()
    myLayer.frame = self.frame
    myLayer.backgroundColor = NSColor.greenColor().CGColor
    self.layer?.addSublayer(myLayer)

那个例子用绿色填充整个标题视图,所以我试图告诉我我想要这样的标题:

self.tableView?.tableColumnWithIdentifier("date")?.title = "Date"

...但是什么也没发生。有人可以帮忙吗?

xcode macos swift nstableview
2个回答
1
投票

在我的一个应用程序中,我想做(几乎)相同的事情,再加上一点(改变 headerView 的高度)。因此,我用我自己的子类从原始单元格复制标题、字体、stringValue 等,对 (不是 NSTableHeaderView 而是)

NSTableHeaderCell
进行了子类化。 (我在 IB 中无法做到这一点。)

我首先以编程方式将 headerView 的所有单元格替换为我的类的对象。在这里(在方法

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
中)我设置了backGroundColor。效果很好。

我的提示:不要在(的子类)中设置backGroundColor

NSTableHeaderView
)但属于
NSTableHeaderCell
的子类。


0
投票
class ViewController: NSViewController {

@IBOutlet weak var listTableView: NSTableView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Set custom header cell class for each table column
    for aColumn in listTableView.tableColumns {
        let customHeaderCell = CustomHeaderCell(textCell: aColumn.headerCell.stringValue)
        customHeaderCell.font = NSFont.systemFont(ofSize: 13, weight: .regular)
        customHeaderCell.textColor = NSColor.white
        aColumn.headerCell = customHeaderCell
    }
  }
}



class CustomHeaderCell: NSTableHeaderCell {

// Override the initializer to set up your custom properties
override init(textCell: String) {
    super.init(textCell: textCell)
    commonInit()
}

required init(coder: NSCoder) {
    super.init(coder: coder)
    commonInit()
}

// Common initialization code
private func commonInit() {
    // Customize any additional properties or settings here
    self.drawsBackground = true
    self.backgroundColor = NSColor.red
}

// Override the drawing method to customize the appearance
override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
    // Draw background color
    self.backgroundColor?.set()
    NSBezierPath(rect: cellFrame).fill()

    // Draw separator line
    NSColor.black.withAlphaComponent(0.2).set()
    NSBezierPath(rect: NSRect(x: cellFrame.origin.x, y: cellFrame.origin.y, width: cellFrame.size.width, height: 1)).fill()

    // Draw header text
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center
    let headerCellText = NSAttributedString(string: stringValue, attributes:
                                            [NSAttributedString.Key.foregroundColor: NSColor.black,
                                             NSAttributedString.Key.font: NSFont.systemFont(ofSize: 13, weight: .regular),
                                             NSAttributedString.Key.paragraphStyle: paragraphStyle,
                                             NSAttributedString.Key.baselineOffset: -6.0
                                            ])
    headerCellText.draw(in: cellFrame)
}
}

参考:https://developer.apple.com/forums/thread/735989

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.