On 29.07.2008, at 18:01, Roland King wrote:

I started over a couple of weeks ago with the new Hillegass book, trying to make sure I understood things properly this time and of course I'm now stuck.

I'm at the 'RaiseMan' chapter now. This is where Hillegass introduces a NSArrayController and its use with an NSTableView. Here are the major pieces.

MyDocument exposes an 'employees' property, a NSMutableArray, that's an array of 'Person' objects, each with a personName and expectedRaise property.

You drop in an NSArrayController and bind its Content Array to the File's Owner.employees. Ok that makes sense, you've bound the content array of the NSArrayController to the NSMutableArray in your document, the model, where all the Person's are held. That's the model connection.

Then you drop an NSTableView onto the document window, plus Add and Delete buttons. You bind the Value of each of the columns of the NSTableView to the arrangedObjects.personName and arrangedObjects.expectedRaise of the NSArrayController .. this sort of makes sense, that's the data for each column.

The Add button triggers the add: action of the NSArrayController and the Delete button triggers the remove: action of the NSArrayController.

'Add' I basically understand. The ArrayController receives an add:, it makes a new Person, adds it to the end of the MutableArray and by the magic of bindings the NSTableColumns find out and the NSTableView gets redrawn.

'Delete' however I don't understand. Hitting the button triggers the remove: action in the NSArrayController which deletes the selected entry from the NSMutableArray but ... how does it know which one to delete? I haven't told the NSArrayController what NSTableView it's attached to, or if it's attached to one at all. I bound the NSTableColumns to arrangedObjects.<key> on the NSArrayController, but that's the wrong way around, and those are the columns anyway, not the NSTableView itself.

In the previous chapter in the book the exercise was basically to write something similar by hand, the NSTableView was an outlet of the controller and when remove: was triggered, you looked up the current selection using the outlet and knew what to delete, made total sense. But with the NSArrayController, there's no outlet, there's no visible binding between the controller and the view, so how does it know what the current selection is?

If you bind a NSTableColumn to an NSArrayController the NSTableView's selectionIndexes-binding is bound automatically to the NSArrayController. (As mentioned in the Binding Reference: <http://developer.apple.com/documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSTableView.html#//apple_ref/doc/uid/NSTableView-DontLinkElementID_621 >)

Now the NSArrayController selection is in sync with the NSTableView and remove: makes sense ...




[ Supplementary question - when used in 'hand written' code you supply a dataSource for the NSTableView. When used with bindings, that's nil. I wondered how that worked too. If there is a nil dataSource does the underlying implementation of the NSTableView assume its NSTableColumns are bound and basically becomes its own dataSource? ]

You are free to use both (bindings and datasource). Drag'n'Drop for example …


Cheers,

felix

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to