There is no “easy” way, you need to go through the nodes of the tree and find the corresponding pointer path, for example:
Objective-C:
Category
@implementation NSTreeController (Additions) - (NSIndexPath*)indexPathOfObject:(id)anObject { return [self indexPathOfObject:anObject inNodes:[[self arrangedObjects] childNodes]]; } - (NSIndexPath*)indexPathOfObject:(id)anObject inNodes:(NSArray*)nodes { for(NSTreeNode* node in nodes) { if([[node representedObject] isEqual:anObject]) return [node indexPath]; if([[node childNodes] count]) { NSIndexPath* path = [self indexPathOfObject:anObject inNodes:[node childNodes]]; if(path) return path; } } return nil; } @end
Swift:
Extension
extension NSTreeController { func indexPathOfObject(anObject:NSObject) -> NSIndexPath? { return self.indexPathOfObject(anObject, nodes: self.arrangedObjects.childNodes) } func indexPathOfObject(anObject:NSObject, nodes:[NSTreeNode]!) -> NSIndexPath? { for node in nodes { if (anObject == node.representedObject as! NSObject) { return node.indexPath } if (node.childNodes != nil) { if let path:NSIndexPath = self.indexPathOfObject(anObject, nodes: node.childNodes) { return path } } } return nil } }
Rob keniger
source share