Re: NSTreeController: how do I select a model object?
Re: NSTreeController: how do I select a model object?
- Subject: Re: NSTreeController: how do I select a model object?
- From: Charles Srstka <email@hidden>
- Date: Tue, 15 Oct 2013 18:51:10 -0500
On Oct 15, 2013, at 5:34 PM, Mike Abdullah <email@hidden> wrote:
> On 14 Oct 2013, at 22:18, Jens Alfke <email@hidden> wrote:
>
>> With an NSOutlineView driven by an NSTreeController, I’m having trouble figuring out how to programmatically select an item: given one of my model objects, how do I tell the controller or view to select it?
>>
>> NSTreeController has a .selectedObjects property, which is great for _getting_ the selection, but for some reason it’s a read-only property (unlike in NSArrayController), so I can’t use it to _set_ the selection.
>>
>> But if I go through the outline view, its ‘item’ objects are the NSTreeNodes managed by the controller, not my model objects, and I don’t see any clean way to map from a model object to its tree node. It seems like I’d have to write a recursive function to search the entire NSTreeNode hierarchy for one whose representedObject is my model object. Surely that can’t be the only way to do this?
>
> Consider NSArrayController. Its -setSelectedObjects: method does nothing more special internally than search through -arrangedObjects looking for the objects in question. Potentially this is pretty slow; an O(n) operation I believe. Those performance characteristics probably feel quite natural to developers I'd say.
>
> Now, for NSTreeController, Apple could have done something similar. They could have implemented -setSelectedObjects to iterate through all the nodes looking for your desired objects and managing the selection from there. However, this is generally going to have similarly poor performance to NSArrayController. Quite likely the overall effect is worse since the reason you're using an outline view is possibly because you have a larger amount of data to display, or that loading chunks of that data carry a greater performance penalty.
>
> Furthermore, there's several approaches that could be taken on such a brute force search too. I suspect Apple chose not to implement this method because of the fundamental performance constraints and uncertainty around it. So:
Well, given that they have access to the source code, Apple could just create an NSMapTable that would map the model objects to the tree nodes associated with them. I actually did a proof of concept of this sometime back around when Leopard was released that accomplished this via categories and method swizzling — it was pretty hacky and fragile, so I never used it in any actual products and have generally resorted to the "go up the ancestor tree" approach, but it did work pretty well. Apple could do it properly if they wanted to. My best guess as to why they don't is because, IIRC, NSTreeController is set up so that the same model objects can reappear at multiple places in the hierarchy, whereas mapping a model object to a node requires a one-to-one correspondence.
Charles
_______________________________________________
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