Editing TableViews with a totals field

I’m using XCode 3.2.3 and I’ve worked through a number of the tutorials as well as looked through the forums and I cannot seem to find an answer to my problem although Craig’s tutorial part 4 is very close to what I’m trying to do (thanks Craig).
What I would like to do is enter data into a tableview using a datasource resulting in at least one column of integers. At that point I would like the user to be able to edit information directly in the tableview so they can adjust some of the numbers. Craig’s tutorial shows how to pull info from the tableview and store it in a separate field and then put it back at the press of a button, but I would actually like to edit the fields in the tableview itself.
I would also like to present a text field where I could show the total for the column of integers so that if the user edits one of the cells it would also be updated in this total field. It seems pretty simple, but I cannot get it to work. When I change a number in my tableview it reverts back to the original number so obviously the totals never get updated either.
How do I get the change to stay and get my totals field to update? Any sample code, help or direction would sure be appreciated. Thanks

I suggest you abandon using a datasource and use an array controller and bindings instead.

Thanks Shane. I’ve abandoned the datasource and am now using an array controller with bindings. I’ve figured out how to load information into the table/array and how to read the information as well. What I cannot figure out is how to auto-update a totals column cell of the table when another table cell has been adjusted. As an example I have a table with three columns each containing numbers. The third column is the sum of the first two columns numbers. I am trying to set it up so that when I edit a number in the first two columns the number in the third column changes to reflect the new sum.
Logically I think tasks to accomplish this are to get the selected row, read it and update it. My problem seems to be that I don’t know how/what would initiate the action or how to update the rows information. Can you point me in the right direction? Please and thank you

One way that I know works is to implement the table data not as a list of records but as a list of objects. This means setting up a separate class for a row and giving it appropriate properties, and setting the class name of the array controller to the row’s class name. You can then implement the summing by either implementing the new column as a handler in the new class or implementing setters for the others that do the sums. A bit hard to describe fully in a few words…

I can’t think of an easy way with records short of, perversely, either using separate fields for editing, or going back to using a datasource and doing the calculations in tableView:setObjectValue:forTableColumn:row:.

Sorry I can’t offer any better advice!

I don’t know if you’re still interested in an answer to this question – I came upon your post while I was looking for something else. You can do this using notifications, specifically the NSControlTextDidEndEditingNotification, which will be sent when you finish editing a cell. Then, using NSNotificationCenter’s defaultCenter’s method, addObserver_selector_name_object_ you can perform the addition and populate your third column. Here is a sample program that implements that:

script TotalsColumnAppDelegate
	property parent : class "NSObject"
	property theTable : missing value
	property theData : missing value
	property arrayController : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		setTheData_(current application's NSMutableArray's arrayWithArray_({{col1:47, col2:14}, {col1:38, col2:9}}))
		current application's NSNotificationCenter's defaultCenter's addObserver_selector_name_object_(me, "makeTotals", current application's NSControlTextDidEndEditingNotification, theTable)
	end applicationWillFinishLaunching_
	
	on makeTotals()
		set rowDict to arrayController's selectedObjects()'s objectAtIndex_(0) --this gets the dictionary for the selected row
		set add1 to rowDict's valueForKey_("col1") as real
		set add2 to rowDict's valueForKey_("col2") as real
		rowDict's setValue_forKey_(add1 + add2, "col3")
	end makeTotals
end script

Like a lot of things that are done with notifications, this can also be done with a delegate. If you include the line, theTable’s setDelegate_(me) in the applicationWillFinishLaunching method, delete the NSNotificationCenter method, and change the name of the makeTotals() method to controlTextDidEndEditing_(aNotification), you get the same results – controlTextDidEndEditing_ is a method of the NSControl class, of which NSTableView is a subclass.

Ric