0

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)
    }
}
Brickers
  • 182
  • 1
  • 12
  • `dataCell` returns `Any` so you have to cast the cell: `(keepOnTopColumn.dataCell as! MyCheckBox).backgroundColor = NSColor.red` – vadian Feb 06 '18 at 05:50
  • I tried a slightly simplified version of this where I downcast to `NSButtonCell`, as all the subclass was doing was trying to set the background colour, so this seemed to duplicate that. It failed, throwing the error 'could not downcast from NSTextFieldCell to NSButtonCell'. The column contains an NSTextFieldCell and an NSButton, and then the NSButton contains the NSButtonCell I want - how do I access that cell? – Brickers Feb 06 '18 at 09:46
  • Is the table view cell based or view based? – vadian Feb 06 '18 at 09:49
  • i would say cell-based as there are plenty of cells in there (sorry, it's a codebase I've not looked at for a while, and I'm not familiar with view based tables). I have heard cells are being deprecated - should I go down the route of view based? – Brickers Feb 06 '18 at 09:53
  • Yes, it's highly recommended to use a view based table view. – vadian Feb 06 '18 at 09:55
  • thanks. how do i go about that? I currently have a table view that i dragged across from the object library, populated with things such as a button, also from the object library. that brings an NSButtonCell with it as a child - how do I go about migrating to view based? edit: looks like this is view based - the overarching table is an `NSTableView` – Brickers Feb 06 '18 at 09:59

0 Answers0