Search code examples
nsoutlineview

How can I ensure that NSOutlineView.parent(forItem:) returns the correct parent object if the same child object exists multiple times?


I have just encountered a problem when using NSOutlineView and wonder is anyone has a solution.

I have a hierarchy of items and the lowest nodes are objects that can appear in more than one location.

For example:

  • Country

    • Town

      • Car Manufacturer
  • USA

    • Chicago

      • Toyota
    • Dallas

      • Toyota

Now if the user selects the 'Toyota' items in the list and if I then try and find the parent for the selected item using the outlineView.parent(forItem:) API it will always return the same parent item, presumably the first one it encounters.

It seems that outlineView does not independently keep track of the parent item when loading data from the data source.

Presumably the only way to overcome this would be to create some sort of unique wrapper object, a struct maybe, at each node to hold the common object.

Anyone have any idea what methods NSOutlineView uses to find the parent of a given object?


Solution

  • From NSOutlineView Class Reference (emphasis mine):

    An outline view has the following features:

    • A user can expand and collapse rows.

    • Each item in the outline view must be unique. In order for the collapsed state to remain consistent between reloads the item's pointer must remain the same and the item must maintain isEqual: sameness.