Search code examples
objective-ccocoansoutlineview

Determine section for selected row from NSOutlineView


OK so I've got a sidebar built using an NSOutlineView. I currently have two sections in the sidebar which can be expanded/contracted. I would like to be able to determine which section the selected row belongs to.

- Section 1
-- Item 1
-- Item 2
-- Item 3
- Section 2
-- Item 4
-- Item 5

The problem is that the selectedRow value changes depending on whether sections are expanded or not. Is there no easy way to determine which section the row belongs to without manually keeping track of expansion/contraction and the number of items in each section?


Solution

  • Try this:

    //returns id of section, where currentRow is a selectedRow
    id section = [yourNSOutlineView parentForItem:[yourNSOutlineView itemAtRow:selectedRow]];