Estoy intentando crear un NSOutlineVew con un grupo de encabezado personalizado (nodo primario) para los objetos enumerados. (NOTA: tengo NSOutlineView basado en celdas). Por ejemplo, se parece a la barra lateral de Xcode "Navegador" o Números. Usé los grupos predeterminados para las propiedades de separación por categoría, pero parece que no es lo que quiero. Necesito un nodo padre (celda), que puedo ajustar visualmente (agregar elementos de control e imagen).Secciones personalizadas para objetos enumerados de matriz en NSOutlineView
Intenté hacer esto pasando una matriz de objetos a NSDictionary, dando a cada grupo una determinada clave específica. Y como resultado, a través de NSLog todo se muestra correctamente, pero la transferencia de esta variable como fuente del programa NSOulineView falla.
ProjectViewController.h
@interface ProjectViewController : NSViewController <NSOutlineViewDataSource, NSObject> {
IBOutlet NSOutlineView *outlineView;
FSEntity *content;
}
@property (readonly, assign) NSMutableArray *objects;
@end
ProjectViewController.m
@implementation ProjectViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
// Setting default path to the local file or directory
NSString *home = NSHomeDirectory();
NSURL *url = [[NSURL alloc] initFileURLWithPath:home];
content = [[FSEntity alloc] initWithURL:url];
[self defineContentNSOutlineView];
NSLog(@"Array: %@",_objects);
// Basic сonfiguration an instance NSOutlineView
[self configurationNSOutlineView];
} return self;
}
@synthesize objects = _objects;
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return (item == nil) ? [content.children objectAtIndex:index] : [((FSEntity *)item).children objectAtIndex:index];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return (item == nil) ? content.children.count > 0 : ((FSEntity *)item).children.count > 0;
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return (item == nil) ? content.children.count : ((FSEntity *)item).children.count;
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if ([item isKindOfClass:[FSEntity class]]) {
return [((FSEntity *)item) title];
}
return nil;
}
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
if ([cell isKindOfClass:[ImageAndTextCell class]]) {
ImageAndTextCell *textField = (ImageAndTextCell *)cell;
[textField setImage:[item icon]];
}
}
- (void)defineContentNSOutlineView {
NSMutableArray *objects = [NSMutableArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"FINDER", @"title", [NSArray arrayWithObjects:[NSDictionary dictionaryWithObject:content.children forKey:@"title"], nil], @"children",[NSNumber numberWithBool:YES], @"header", nil], nil];
_objects = objects;
}
- (void)configurationNSOutlineView {
[outlineView sizeLastColumnToFit];
[outlineView setFloatsGroupRows:NO];
[outlineView reloadData];
[outlineView expandItem:nil expandChildren:YES];
}
@end
Para más fácil sería imaginar cómo se vería, me lo mostró en el esquema:
+--------------------------------------------+
| ▼ FINDER FILES ₪ ✱ |
| 03143553.file |
| ▶ Desktop |
| ▶ Documents |
| ▶ Downloads |
| ▶ Movies |
| ▶ Music |
| ▶ Pictures |
+--------------------------------------------+
y lo que tengo ahora (NSOulineView sin usar NSTreeController);
+--------------------------------------------+
| 03143553.file |
| ▶ Desktop |
| ▶ Documents |
| ▶ Downloads |
| ▶ Movies |
| ▶ Music |
| ▶ Pictures |
+--------------------------------------------+
que sé sobre el ejemplo de Apple "SourceView", pero no sé cómo agregar al grupo creado, matriz de objetos (archivos y carpetas), presentación de NSTreeContoller sólo los primeros elementos de la jerarquía (sin incluye):
+--------------------------------------------+
| ▼ FINDER FILES |
| 03143553.file |
| Desktop |
| Documents |
| Downloads |
| Movies |
| Music |
| Pictures |
+--------------------------------------------+
método modificado de ejemplo SourceView:
- (void)addFinderSection {
[self addFolder:@"FINDER FILES"];
NSError *error = nil;
NSEnumerator *urls = [[[NSFileManager defaultManager] contentsOfDirectoryAtURL:self.url includingPropertiesForKeys:[NSArray arrayWithObjects: nil] options:(NSDirectoryEnumerationSkipsHiddenFiles) error:&error] objectEnumerator];
for (NSURL *url in urls) {
BOOL isDirectory;
if ([[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory]) {
if (isDirectory) {
[self addChild:[url path] withName:NO selectParent:YES];
} else {
[self addChild:[url path] withName:NO selectParent:YES];
}
}
}
[self selectParentFromSelection];
}
Este método muestra sólo los primeros objetos, como se muestra en el último esquema.
Y una pregunta más, como he dicho antes, cómo agregar al nodo ** "FINDER FILES" ** botón en el lado derecho de la celda.
¿Me pueden ayudar con esto? Lo sé, tal vez no sea tan difícil, pero acabo de empezar a aprender Objective-C y no sé cómo hacer esto. Gracias.
Me doy cuenta de que esto probablemente no sea del todo útil, pero sospecho que esto sería mucho más fácil de lograr con un NSOutlineView basado en NSView (en comparación con NSCell-based). La razón es que con NSOutlineViews basado en vistas, puede agregar cualquier cantidad de subvistas y conservar todas sus funcionalidades estándar. Con un enfoque basado en la célula, se encontrará con una sola célula, y para combinar los comportamientos de varios controles se necesitará escribir una subclase NSCell personalizada y un gran número de códigos personalizados de dibujo y manejo de eventos. View-based NSOutlineView te dará eso gratis. – ipmcc