对 NSTableView 列的文本字段的所有值求和
Sum all values of a textfield of a NSTableView column
我使用 swift 3,我有一个 NSTableView(3 列)。
我填写如下数据:
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]?
的值的增量
例如:
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
}
因此,在 viewWillLayout()
您可以使用一些标签显示 sum
变量的值。
吉林大学
我使用 swift 3,我有一个 NSTableView(3 列)。 我填写如下数据:
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]?
例如:
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
}
因此,在 viewWillLayout()
您可以使用一些标签显示 sum
变量的值。
吉林大学