On 15 Aug 2008, at 22:37, Antonio Nunes wrote:

I search for an item that belongs to a tree that is shown in an NSOutlineView. When I find the item, I want the outline view to show the it (if it is currently collapsed) and select its row. For that I wrote the following code, that should ensure the selected item's ancestry is expanded, and then selects the targeted item:

                while (currentOutline.parent != nil) {
                currentOutline = currentOutline.parent;
                [clerkDocument.outlinesOutlineView expandItem:currentOutline];
        }
                
[clerkDocument.outlinesOutlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[clerkDocument.outlinesOutlineView rowForItem:match]] byExtendingSelection:NO];

Running this through the debugger I checked that each ancestor of the item gets expanded, and the code executes correctly. However, if several of the ancestors were collapsed, then after this code above is executed the outline only ends up expanding the top previously expanded ancestor, and the targeted item doesn't get selected (because there is no row for item "match" since it is not visible). All the ancestors have been set to expand though.


Almost immediately after posting this, it occurred to me that maybe the more nested ancestors were not being expanded because their parent wasn't (yet) expanded. So I tried putting collecting all ancestors and then looping through them from the top down, marking each as expanded. And by golly this works. I didn't find any (obvious) place in the docs though that claims that expanding an item whose parent is collapsed will be ignored. But this seems to be the case.

The following snippet of code does the trick:

                NSMutableArray *a = [NSMutableArray array];
                while (currentOutline.parent != nil) {
                        currentOutline = currentOutline.parent;
                        [a addObject:currentOutline];
                }

                for (PDFOutline *o in a.reverseObjectEnumerator) {
                        [clerkDocument.outlinesOutlineView expandItem:o];
                }
                
[clerkDocument.outlinesOutlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[clerkDocument.outlinesOutlineView rowForItem:match]] byExtendingSelection:NO];

This is great as far as getting things working goes, but I still find it strange that can't move up the ancestry to expand items, and am forced to process along the opposite direction. And as long as I'm on the subject, I think a method ala "expandItem:expandAncestors:" would be very convenient to do just that. So I guess that will turn into an enhancement request.

António
----------------------------------------------------
There is nothing as strong as real gentleness, and
there is nothing as gentle as real strength.
----------------------------------------------------




_______________________________________________

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