objective-ccocoanstableview

Calculate/Display NSTableView column sum


I’m updating an Objective-C app. I have an NSTableView w/ 2 columns. Column 2 binding: set to arrayController, arrangedObjects, and the Model Key is set to a defined Entity Attribute.

I have Attribute type set to “Float.” I added a numberFormatter to the column with its format set to “Currency.” Everything (Add, Delete, Save, etc.) works as expected. What I would like to do is update an independent textField below the table column to display the running sum of the currency values populating the column.

The updating in the sum textField doesn’t need to update dynamically. I can set up a separate action to manually initialize a calculation of the existing column values.

How can I get this done?


Solution

  • Bind the text field to the array controller

    Controller key: arrangedObjects

    Model Key Path: @sum.modelKey

    The sum value is the same type as the summed values. Add a number formatter to the text field with the same settings as the formatter of the cell in the column.