Change color of NSTableViewCell

后端 未结 4 429
粉色の甜心
粉色の甜心 2021-01-17 17:08

How can I change the color of a cell in my NSTableView?

4条回答
  •  执念已碎
    2021-01-17 17:18

    // TESTED - Swift 3 solution...for changing color of cell text in a single column. All columns in my tableview have a unique identifier

        func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    
            let myCell:NSTableCellView = tableView.make(withIdentifier: (tableColumn?.identifier)!, owner: self) as! NSTableCellView
            if tableColumn?.identifier == "MyColumn" {
                let results = arrayController.arrangedObjects as! [ProjectData]
                let result = results[row]
                if result.ebit < 0.0 {
                    myCell.textField?.textColor = NSColor.red
                } else {
                    myCell.textField?.textColor = NSColor.black
                }
            }
            return myCell
        }
    

提交回复
热议问题