I am attempting to set the background colour of a checkbox within a tableview, but am struggling. currently I have subclassed NSButtonCell
as follows:
import Cocoa
class MyCheckBox: NSButtonCell {
override init(textCell string: String) {
super.init(textCell: string)
self.backgroundColor = NSColor.red
}
required init(coder: NSCoder) {
super.init(coder: coder)
}
}
and then, in the identity inspector, I have set the checkbox's custom class to be MyCheckBox
, however it does not seem to be working. I'm not sure whether assigning to self.backgroundColor
is the correct way to set the background color, or whether using the identity inspector is the way to get each checkbox to use the correct background color, and I am struggling once more with the sheer volume of Apple docs about where to find this information.
I have also tried creating an outlet for this checkbox column (named keepOnTopColumn
) in the view controller, and attempted to get my custom class working that way:
let str: String = ""
keepOnTopColumn.dataCell = MyCheckBox(textCell: str)
I have also tried to access the background color directly:
keepOnTopColumn.dataCell.backgroundColor = NSColor.red
but this results in an error "Value of type 'Any' has no member 'backgroundColor'"
finally, I have also tried to affect the parent NSButton of the cell:
class MyCheckButton: NSButton {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.wantsLayer = true
self.layer?.backgroundColor = NSColor.red as! CGColor
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}