Re: Select model objects with NSTableView using cocoa bindings
Re: Select model objects with NSTableView using cocoa bindings
- Subject: Re: Select model objects with NSTableView using cocoa bindings
- From: Quincey Morris <email@hidden>
- Date: Wed, 12 Oct 2011 14:22:37 -0700
On Oct 12, 2011, at 13:31 , Luc Van Bogaert wrote:
> I'm wondering if I should create a NSIndexSet property in my model object and bind it to the NSArrayController's 'selectedIndexes' key?
Yes, but you've got the terminology wrong. The relevant concept here is a binding name, and it's "selectionIndexes", not "selectedIndexes". NSArrayController also has a "selectionIndexes" property, but that's what the table view binds to, and is not involved here (except as an "intermediary" property between the table view and the data model -- which is why a NSArrayController is a "mediating" controller.) Also, binding actually goes in the other direction -- you bind the object with the binding (the array controller) to the object with the property (the model). But I'm pretty sure that's what you meant. :)
Now that I think about it, the correct place for the NSIndexSet property is your window controller (or whatever the window's nib's File's Owner object is), *unless* one of the following design considerations applies:
1. You intend to save to selection state to disk with the rest of the data model. In that case, the selection status is truly a data model property, not a UI-related property.
2. You have multiple windows onto the same data model, and you want them all to have the same selection, even if you don't want to save the selection status to disk.
> But then, I should still somehow 'translate' the content of this indexset to the individual Dot (or DotController) objects in my model array…
There are various easy, reliable ways of doing this. The simplest is probably to implement the "tableViewSelectionDidChange:" method in your table view delegate (typically the window controller or File's Owner). Conceptually, all you need to do is loop through the Dot objects, and tell each one (a) its new selection status [YES or NO] and (b) setNeedsDisplay:YES. In practice, you'd fix that so as not to message or redraw the Dots whose state didn't change.
> Or, should I be able to use a BOOL 'selected' property of my DotController object and bind it directly to the NSArrayController's selectedIndexes?
Well, you can't *bind* a binding that requires to a NSIndexSet property to a BOOL property, or whatever you're suggesting here. I'd recommend the brute force approach I already described, where you just adjust the individual selection statuses yourself. Also, I'd likely put the "selected" property on the Dot, not the DotController, but the answer will depend on the details of your application.
You can also do without an ivar-backed "selected" Dot property if you want. The Dot can presumably find out what its own DotController index is, and so can presumably find out whether its index is in the NSIndexSet property. In that case, you don't have to tell the Dot its status, you just tell it to redraw and it figures out the status at redraw time. Either way is fine.
> I'm also wondering what would be needed, in this context, to get the Dot views to redraw themselves once their 'selected' property has been changed...
Again, brute force (as described above, in the delegate method) is the simplest, most direct approach: just tell them.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden