5

I work with swift 3 and I have a NSTableView (3 columns). I fill the data like 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 sum all values of column 1, every time when the selection will change. how can I realize this?

Alwin
  • 1,476
  • 3
  • 19
  • 35
Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • Considering that every cell shows “100”, the answer is always `100 * tableView.numberOfRows`. Would you like to edit your answer to show how your cells **actually** get their values, so you can get actual advice? – rob mayoff May 28 '17 at 19:50
  • the cellText value is not every time 100 (only in the example). each cell can have a different value – Trombone0904 May 28 '17 at 20:18
  • 2
    Is the data that's getting set in the cells in a datasource somewhere? Or is it hardcoded, as in the example? It would be better to store the values in an array or dictionary and sum from there, than try to reverse engineer a value from the cells themselves. – Stephen Jun 01 '17 at 15:48

1 Answers1

1

To add the values, you must guarantee that all the values are numbers or, at least, could be converted to numbers.

After that, its necessary to maintain an variable receiving the increment of the 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, at viewWillLayout() you can show the value of sum variable using some label.

JLU.