How to select items in NSOutlineView without NSTreeController?

cocoafan picture cocoafan · Jul 8, 2009 · Viewed 12.5k times · Source

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.

Answer

Peter Hosey picture Peter Hosey · Jul 8, 2009

Remember to look in superclasses when you can't find something. In this case, one of the methods you need comes from NSTableView, which is NSOutlineView's immediate superclass.

The solution is to get the row index for the item using rowForItem:, and if it isn't -1 (item not visible/not found), create an index set with it with [NSIndexSet indexSetWithIndex:] and pass that index set to the selectRowIndexes:byExtendingSelection: method.