OK, entonces quiere un NSOutlineView
con ImageAndTextCell
celdas, ¿verdad?
Hagamos uno de los ejemplos más típicos de este tipo: un simple explorador de archivos.
Lo que vamos a necesitar:
- un
NSOutlineView
(poner un esquema para su AppDelegate, como fileOutlineView
)
- crear 3 columnas en el esquema con los siguientes identificadores (configurarlas en la interfaz constructor):
NameColumn
, SizeColumn
, ModifiedColumn
Ahora, en cuanto al resto, lo haré todo mediante programación, por lo para tener una buena idea de lo que está pasando ...
Cómo configurarlo (p. ej. en - (void)awakeFromNib
):
// set the Data Source and Delegate
[fileOutlineView setDataSource:(id<NSOutlineViewDataSource>)self];
[fileOutlineView setDelegate:(id<NSOutlineViewDelegate>)self];
// set the first column's cells as `ImageAndTextCell`s
ImageAndTextCell* iatc = [[ImageAndTextCell alloc] init];
[iatc setEditable:NO];
[[[fileOutlineView tableColumns] objectAtIndex:0] setDataCell:iatc];
Conectando los puntos:
/*******************************************************
*
* OUTLINE-VIEW DATASOURCE
*
*******************************************************/
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
if ([item isFolder])
return YES;
else
return NO;
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if (item==nil)
{
// Root
return [[filePath folderContentsWithPathAndBackIgnoringHidden] count];
}
else
{
if ([item isFolder])
{
return [[item folderContentsWithPathAndBackIgnoringHidden] count];
}
else
{
return 0;
}
}
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
if (item == nil)
{
// Root
return [[filePath folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
}
if ([item isFolder])
{
return [[item folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
}
// File
return nil;
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item
{
if ([[theColumn identifier] isEqualToString:@"NameColumn"])
{
return [item lastPathComponent];
}
else if ([[theColumn identifier] isEqualToString:@"SizeColumn"])
{
if ([item isFolder]) return @"--";
else return [NSString stringWithFormat:@"%d",[item getFileSize]];
}
else if ([[theColumn identifier] isEqualToString:@"ModifiedColumn"])
{
if ([item isFolder]) return @"";
else return [NSString stringWithFormat:@"%@",[item getDateModified]];
}
// Never reaches here
return nil;
}
/*******************************************************
*
* OUTLINE-VIEW DELEGATE
*
*******************************************************/
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
return YES;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
{
return NO;
}
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
[cell setDrawsBackground:NO];
if ([item isFileHidden]) [cell setTextColor:[NSColor grayColor]];
else [cell setTextColor:[NSColor whiteColor]];
if ([[tableColumn identifier] isEqualToString:@"NameColumn"])
{
if ([item isFolder])
[cell setImage:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] size:15.0];
else
[cell setImage:[[NSWorkspace sharedWorkspace] iconForFile:item] size:15.0];
if ([item isFileHidden])
{
[cell setFileHidden:YES];
}
else
{
[cell setFileHidden:NO];
}
}
}
Consejo:ImageAndTextCell
clase se puede encontrar here. También se dará cuenta algunos otros métodos que estoy usando, lo que obviamente no son soportados por Cacao (por ejemplo isFileHidden
, isFolder
o folderContentsWithPathAndBackIgnoringHidden
), pero no es tan difícil de aplicar por sí mismo ...)
No veo cómo es esto correcto. ImageAndTextCell es del tipo NSCell, NO NSView. La pregunta era sobre una vista basada en NSOutlineView. Su ejemplo es simplemente crear una subclase de NSCell personalizada que hemos podido hacer durante años. Para NSTableView hay muchos ejemplos de cómo usar NSView en lugar de NSCell. Se parece mucho a iOS. No he encontrado ningún ejemplo de NSOutlineView con tristeza. –
@ Las tablas basadas en Adam-view usan NSTableCellViews como sus vistas. Desde allí, puede agregarles cualquier contenido. Lo que el Dr.Kameleon ha dicho es exactamente correcto. El conjunto de tablas basadas en el punto de vista es que puede agregar cualquier objeto de vista que desee (ya sea una celda, botón, imagen bien ... lo que sea) – Patrick
No se trata de crear una vista de esquema basada en vista. – JeremyP