Macos NSTableView列的textfield的所有值总和
我使用swift 3,我有一个NSTableView(3列)。 我填写如下数据:Macos NSTableView列的textfield的所有值总和,macos,swift3,xcode8,Macos,Swift3,Xcode8,我使用swift 3,我有一个NSTableView(3列)。 我填写如下数据: func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { var cellIdentifier = String() var cellText = String() switch tableColumn {
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
var cellIdentifier = String()
var cellText = String()
switch tableColumn {
case tablewView.tableColumns[0]?:
cellText = "100"
cellIdentifier = "Cell1"
break
case tablewView.tableColumns[1]?:
cellText = "100"
cellIdentifier = "Cell2"
break
case tablewView.tableColumns[2]?:
cellText = "100"
cellIdentifier = "Cell3"
break
default: break
}
if let view = tableView.make(withIdentifier: cellIdentifier, owner: nil) as? NSTableCellView {
view.textField?.stringValue = cellText
return view
}
return nil
}
现在,每当选择发生变化时,我想对第1列的所有值求和。如何实现这一点?要添加值,必须保证所有值都是数字,或者至少可以转换为数字 之后,需要维护一个变量,该变量从
tablewView.tableColumns[1]?
例:
因此,在viewWillLayout()
中,可以使用一些标签显示sum
变量的值
JLU.考虑到每个单元格都显示“100”,答案总是
100*tableView.numberOfRows
。是否要编辑答案以显示单元格实际获取值的方式,以便获得实际建议?cellText值不是每次100(仅在示例中)。每个单元格都可以有不同的值数据源中单元格中设置的数据是否在某个位置?或者它是硬编码的,如示例中所示?最好将值存储在数组或字典中并从中求和,而不是尝试对单元格本身的值进行反向工程。
var sum = 0
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
var cellIdentifier = String()
var cellText = String()
switch tableColumn {
case tablewView.tableColumns[0]?:
cellText = "100"
cellIdentifier = "Cell1"
break
case tablewView.tableColumns[1]?:
cellText = "100"
sum = sum + Int(cellText)
cellIdentifier = "Cell2"
break
case tablewView.tableColumns[2]?:
cellText = "100"
cellIdentifier = "Cell3"
break
default: break
}
if let view = tableView.make(withIdentifier: cellIdentifier, owner: nil) as? NSTableCellView {
view.textField?.stringValue = cellText
return view
}
return nil
}