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: Mike Abdullah <email@hidden>
- Date: Tue, 15 Oct 2013 23:34:10 +0100
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:
You could write your own routine that traverses all the nodes. This could be optimised for a few different search patterns, such as initially only looking at the rows currently loaded in your outline view, or even just those onscreen.
Or, if your model includes relationships back up to the "parent" of the objects, you can use this to considerably optimise the search:
1. Build up a list of ancestors
2. Search the root node for the farthest ancestor
3. Search that node for the next ancestor, and so on
We take this approach in Sandvox and it works pretty well.
_______________________________________________
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