Sum all text field values ​​of an NSTableView column

I am working with fast 3 and I have NSTableView (3 columns). I fill in the data as below:

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 } 

Now I would like to summarize all the values ​​of column 1, every time the choice changes. how can i understand that?

+5
source share
1 answer

To add values, you must ensure that all values ​​are numbers, or at least can be converted to numbers.

After that, is it necessary to maintain a variable that receives the increment of values ​​from tablewView.tableColumns[1]?

ex:

 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 } 

So, in viewWillLayout() you can show the value of the sum variable using some label.

JLU.

+1
source

Source: https://habr.com/ru/post/1268175/


All Articles