Anybody have any good suggestions for getting an NSColorWell into an NSTableView? Most other widgets work great, since thery're based around cells, but NSColorWell doesn't have a corresponding NSColorWellCell.
NSColorWell in an NSTableView in swift
515 views Asked by thierryH At
2
There are 2 answers
2
On
I solved my problem without Binding Cocoa I updated the view and everything works fine by upgrading NSTableCellView (the last 3 lines) in the real program which is a program of account there is too much relation between the tableview and the outlineview, the filters that made me redo a lot of work by making the binding
Thanks for your help
class ColorsController: NSWindowController {
@IBOutlet var colorTable: NSTableView!
let tableViewData =
[["firstName":"John","lastName":"Doe","emailId":"[email protected]"],
["firstName":"Jane","lastName":"Doe","emailId":"[email protected]"]]
var color = [NSColor.red, NSColor.blue]
override func windowDidLoad() {
super.windowDidLoad()
self.colorTable.reloadData()
}
@IBAction func actionColorWell(_ sender: NSColorWell) {
let row = colorTable.row(for: sender as NSView)
color[row] = sender.color
colorTable.reloadData()
let select1 : IndexSet = [row]
colorTable.selectRowIndexes(select1, byExtendingSelection: false)
}
}
extension ColorsController : NSTableViewDataSource, NSTableViewDelegate{
func numberOfRows(in tableView: NSTableView) -> Int {
return tableViewData.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?{
let identifier = (tableColumn?.identifier)!
switch identifier.rawValue {
case "firstName" :
let result = tableView.makeView(withIdentifier: identifier, owner: self) as! NSTableCellView
result.textField?.stringValue = tableViewData[row][(tableColumn?.identifier.rawValue)!]!
result.textField?.textColor = color[row]
return result
case "lastName" :
let result = tableView.makeView(withIdentifier: identifier, owner: self) as! NSTableCellView
result.textField?.stringValue = tableViewData[row][(tableColumn?.identifier.rawValue)!]!
result.textField?.textColor = color[row]
return result
case "emailId" :
let result = tableView.makeView(withIdentifier: identifier, owner: self) as! KSDataCellView
result.textField?.stringValue = tableViewData[row][(tableColumn?.identifier.rawValue)!]!
result.textField?.textColor = color[row]
result.colorWell.color = color[row]
return result
case "color" :
let result = tableView.makeView(withIdentifier: identifier, owner: self) as! NSColorWell
result.color = color[row]
return result
default:
return nil
}
}
}
class KSDataCellView: NSTableCellView {
@IBOutlet weak var colorWell:NSColorWell!
}
In a view based table view the most convenient solution is Cocoa Bindings. You can bind the
value
of the color well to anNSColor
instance of your model.Without Cocoa Bindings create an
IBAction
method in the target view controllerIn Interface Builder control drag from the color well to the view controller and connect the action. The action will print
row
,column
and the new color.tableView
is theNSTableView
outlet.If there are multiple color wells in the same view you could assign different tags to distinguish the color wells