Alex Rivera | Logout

How to select items in NSOutlineView without NSTreeController?

Asked 2009-07-08T08:18:32.500
14

I'm using NSOutlineView without NSTreeController and have implemented my own datasource. What is the best way to select an item? NSOutlineView support already expandItem: and collapseItem:. And I'm missing a handy method like `selectItem:. How can I do it programatically ?

Thank you.

Edit
Report

1 Answer

2

No, there isn't a selectItem: method, but there is an rowForItem: method. If you combine that with Peter's advice about using selectRowIndexes:byExtendingSelection: above, you should have all the information you need.

If you really wanted to have a method to select an item, which I would recommend calling setSelectedItem: for consistency's sake, you could write something like this in a category on NSOutlineView

- (void)setSelectedItem:(id)item {
    NSInteger itemIndex = [self rowForItem:item];
    if (itemIndex < 0) {
        // You need to decide what happens if the item doesn't exist
        return;
    }

    [self selectRowIndexes:[NSIndexSet indexSetWithIndex:itemIndex] byExtendingSelection:NO];
}

I have no idea if this code actually works; I just dashed it off to illustrate the concept.

answered 2009-07-08T14:31:30.017

Your Answer