4

After poring through the documentation, I can find no reasonably easy way to get the index path of an object in an NSOutlineView or the NSTreeController it's bound to. As a result, I've ended up writing really ugly code trying to assemble an index path myself whenever I need to do something that requires them (removing specific items from the tree, for instance).

Is there no better way to do this than [[NSIndexPath indexPathWithIndex:<blah>] indexPathByAddingIndex: <blah>]?

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
John Wells
  • 1,139
  • 1
  • 10
  • 27
  • The linked answer only applies to an `NSTreeController` what's the solution for an `NSOutlineView` or dataSource ? – silicontrip Jan 11 '21 at 04:43

1 Answers1

2

You can easily build a path like so:

NSUInteger indexes[4] = {2, 3, 1, 0};
NSIndexPath* path = [NSIndexPath indexPathWithIndexes:indexes length:4];

To get the index path of a particular model object, have a look at my answer to this question.

Community
  • 1
  • 1
Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
  • 2
    `[_treeController.arrangedObjects descendantNodeAtIndexPath:[NSIndexPath indexPathWithIndex:[someOutline selectedRow]]]` has to be one of _the grossest_ constructs / _bungles of an API_ that I have ever witnessed. Apple should be *ashamed* of themselves. – Alex Gray Oct 26 '13 at 03:47
  • 1
    Correctamundo. `NSTreeController` in general is not Apple's finest hour. – Rob Keniger Oct 28 '13 at 00:29
  • 1
    Wait, it's even worse.. just to get the "value" of the highlighted node in a "Tree Controlled" situation requires.. `[[[_treeController.arrangedObjects descendantNodeAtIndexPath:[NSIndexPath indexPathWithIndex:[sender selectedRow]]] representedObject] representedObject]` Lol. That double `representedObject` _drives me bananas_! It's like they were on crack... "Hey let's call it `objectValue`", "NO, I say `representedObject`, whhaaa!" ... "How about `selectionIndexes`", "No `selectedIndexPath`", "No, `NSOutlineViewSelectedPathIndexesPathBindingOption`! That's got a great ring to it." Ha! – Alex Gray Oct 30 '13 at 08:30