简体   繁体   English

如何将NSTableCellView Checkbox切换事件与其数据源相关联?

[英]How do I associate a NSTableCellView Checkbox toggle-event with its data source?

Environment: View-based NSTableView WITHOUT Bindings. 环境:基于视图的NSTableView WITHOUT绑定。
Problem: Mapping column's checkbox action to its datasource 问题:将列的复选框动作映射到其数据源
(ie, how do I know which row the checkbox event has occurred, in real time?). (即,我如何实时知道复选框事件发生在哪一行?)。

Without using IB bindings, how do I associate any particular NSButton/Checkbox action within its respective NSTableView row to its data source? 在不使用IB绑定的情况下,如何将其相应NSTableView行中的任何特定NSButton / Checkbox操作与其数据源相关联?

在此处输入图片说明

I want to be able to update the checkbox's respective data array with its UI state (check in box -> 1 in data source). 我希望能够使用其UI状态更新复选框的相应数据数组(在数据源中选中复选框-> 1)。

That is, mapping an array of structs or closures with their 1:1 table entries. 也就是说,用其1:1表条目映射结构或闭包的数组。 Hence, clicking on a 'select'/row updates its respective boolean flag with the data source array. 因此,单击“选择” /行将用数据源数组更新其各自的布尔标志。

The following code snippet shows how I populate the UI: 以下代码段显示了如何填充UI:

typealias GeoTuple = (geoDesc:String,geoSelected:Int)
var geoList = [GeoTuple]()


func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {

    var cellView:NSTableCellView?
    let cellID = tableColumn?.identifier

    guard nil != cellID else {
        cellView = NSTableCellView()
        return cellView
    }

    cellView = (tableView.makeViewWithIdentifier(cellID!, owner: self) as? NSTableCellView)

    guard nil != cellView else {
        print("{ODYInitializePricingProductViewController} *** NO CELL VIEW GENERATED *** \n Check cell identifiers.")
        return nil
    }

    let currentTableColumn = ODYInitPricingTableColumnIdentifier(rawValue:cellView!.identifier!)

    switch currentTableColumn! {
...
   case .geoSelectFlag:
        if row < geoList.count  {
            if let selectButton = cellView?.viewWithTag(1) as? NSButton {
                selectButton.integerValue = geoList[row].geoSelected
            }
        }
  ...
  return cellView
}

Question: How do I trap for a specific NSButton-Check toggle event, and associate/map this event to its respective data source: geoList? 问题:如何捕获特定的NSButton-Check切换事件,并将该事件关联/映射到其各自的数据源:geoList?

Connect the checkbox button's target to your table view delegate and set its action to the selector of some action method. 将复选框按钮的目标连接到表视图委托,并将其动作设置为某些动作方法的选择器。

It's probably easiest to set the target and action programmatically in tableView(_:viewForTableColumn:row:) . tableView(_:viewForTableColumn:row:)编程方式设置目标和操作可能是最简单的。 Because the cell view hierarchies within table columns are actually in sub-nibs, connecting them to the objects in your NIB or storyboard scenes can be problematic. 由于表列中的单元格视图层次实际上位于子顶点中,因此将它们连接到NIB或情节提要场景中的对象可能会出现问题。

In the action method, you can look up the row by calling rowForView() on the table, passing the sender which the action method receives as a parameter. 在action方法中,您可以通过在表上调用rowForView()并传递该action方法接收的sender作为参数来查找行。 You can also check the sender's state to determine whether the new value for your data model should be on or off. 您还可以检查发送者的state以确定数据模型的新值是打开还是关闭。

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? { ...
...
case .geoSelectFlag:
    if row < geoList.count  {
        if let selectButton = cellView?.viewWithTag(1) as? NSButton {
            selectButton.integerValue = geoList[row].geoSelected
            selectButton.target = self
            selectButton.action = "checkBoxAction:"
        }
    }
...
}

func checkBoxAction(sender:NSButton) {

}

(lldb) po self.geoTableView.rowForView(sender) 3 (lldb)po self.geoTableView.rowForView(sender)3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM