cocoa nsindexpath nstreecontroller

cocoa - Objeto de modelo dado, ¿cómo encontrar la ruta de índice en NSTreeController?



nsindexpath (2)

Dados los objetos de modelo que representa NSTreeController , ¿cómo se encuentran las rutas de índice en el árbol y luego se seleccionan? Este parece ser un problema deslumbrantemente obvio, pero parece que no puedo encontrar ninguna referencia al mismo. ¿Algunas ideas?


¿Por qué no usar NSOutlineView para obtener elementos principales como este?

NSMutableArray *selectedItemArray = [[NSMutableArray alloc] init]; [selectedItemArray addObject:[self.OutlineView itemAtRow:[self.OutlineView selectedRow]]]; while ([self.OutlineView parentForItem:[selectedItemArray lastObject]]) { [selectedItemArray addObject:[self.OutlineView parentForItem:[selectedItemArray lastObject]]]; } NSString *selectedPath = @"."; while ([selectedItemArray count] > 0) { OBJECTtype *singleItem = [selectedItemArray lastObject]; selectedPath = [selectedPath stringByAppendingString:[NSString stringWithFormat:@"/%@", singleItem.name]]; selectedItemArray removeLastObject]; } NSLog(@"Final Path: %@", selectedPath);

Esto generará: ./item1/item2/item3 / ...

Supongo que está buscando una ruta de archivo aquí, pero puede ajustar lo que su origen de datos pueda representar.


No hay una manera "fácil" de recorrer los nodos de los árboles y encontrar una ruta de índice coincidente, algo así como:

C objetivo:

Categoría

@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

Rápido:

Extensión

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 } }