1

有人对将 NSColorWell 放入 NSTableView 有什么好的建议吗?大多数其他小部件工作得很好,因为它们基于单元格,但 NSColorWell 没有相应的 NSColorWellCell。

4

2 回答 2

0

我在没有绑定 Cocoa 的情况下解决了我的问题我更新了视图,并且通过在实际程序中升级 NSTableCellView(最后 3 行)一切正常,这是一个帐户程序 tableview 和 outlineview 之间的关系太多,过滤器我通过绑定重新做了很多工作

谢谢你的帮助

class ColorsController:  NSWindowController  {

@IBOutlet var colorTable: NSTableView!

let tableViewData =
    [["firstName":"John","lastName":"Doe","emailId":"john.doe@mail.com"],
     ["firstName":"Jane","lastName":"Doe","emailId":"jane.doe@mail.com"]]

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!
}
于 2017-12-02T06:13:52.083 回答
0

基于视图的表格视图中,最方便的解决方案是 Cocoa Bindings。您可以将value颜色很好地绑定到NSColor模型的实例。

IBAction没有 Cocoa 绑定在目标视图控制器中创建一个方法

@IBAction func colorDidChange(_ sender: NSColorWell)
{
    let row = tableView.row(for: sender)
    let column = tableView.column(for: sender)
    print(row, column, sender.color)
}

在 Interface Builder 控件中,从颜色井拖动到视图控制器并连接操作。动作将打印rowcolumn新颜色。tableViewNSTableView出口。

如果同一视图中有多个颜色孔,您可以分配不同的标签来区分颜色孔

于 2017-12-01T12:02:02.337 回答