2012-10-08 46 views
6

Me gustaría crear una NSTableview con NSTableCellViews personalizadas.NSTableView personalizado con NSTableCellView personalizado?

Aquí es lo que tengo en este momento:

  • Un archivo semilla para la célula (vista mina) llama CustomCell.xib
  • una clase personalizada para mi celular llamada CustomCell
  • Y el código en mi AppDelegate.m:

H antes de que yo creo mi vista de tabla mediante programación:

NSScrollView *tableContainer = [[NSScrollView alloc]initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)]; 
    NSTableView *tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)]; 

    NSTableColumn *firstColumn = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease]; 
    [[firstColumn headerCell] setStringValue:@"First Column"]; 
    [tableView addTableColumn:firstColumn]; 

    tableView.dataSource = self; 
    tableView.delegate = self; 
    [tableContainer setDocumentView:tableView]; 
    tableContainer.autoresizingMask = NSViewHeightSizable | NSViewMinXMargin; 
    [self.window.contentView addSubview: tableContainer]; 

Y aquí es el método delegado donde me gustaría poner mi código de celda personalizado:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { 


    // In IB the tableColumn has the identifier set to the same string as the keys in our dictionary 
    NSString *identifier = [tableColumn identifier]; 

    if ([identifier isEqualToString:@"myCell"]) { 

     // We pass us as the owner so we can setup target/actions into this main controller object 
     CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self]; 
     // Then setup properties on the cellView based on the column 
     cellView.textField.stringValue = @"Name"; 
     return cellView; 
    } 
    return nil; 
} 

En el archivo semilla para mi celular a medida que he enganchado la vista de celda con mi clase personalizada llamada CustomCell cuyas subclases NSTableCellView. No he hecho ningún otro paso como por ahora. Entonces mi CustomCell.m es solo el código de inicialización predeterminado. No lo he tocado Y no hice nada más en mi archivo de punta, así que no cambié el propietario del archivo ni nada de eso porque realmente no sé qué hacer. ¿Alguien puede ayudarme? Miré los archivos de muestra de la documentación de Apple, pero después de días de investigación no encontré ninguna solución. Realmente apreciaría si pudieras ayudarme.

+0

¿Puede aclarar cuál es el problema? He leído tu publicación un par de veces y no puedo discernir qué problema tienes. – FluffulousChimp

+0

Tengo un NSTableView y quiero celdas con una apariencia personalizada. Así que configuré un archivo de vista de punta donde solo tengo esta celda NSTableCellView. Ahora, quiero programar mi NSTableView para mostrar esas celdas particulares desde el archivo nib. Sin embargo, el código anterior realmente no cambia nada en mi tabla. Tiene sentido ? –

+0

¿resolviste esto? He tropezado con el mismo problema ... – Gossamer

Respuesta

3

Esto es lo que terminé haciendo:

Por supuesto que tiene que subclase NSTableCellView y devolverlo como lo hice a continuación. Si está familiarizado con las vistas de tabla en iOS debe estar familiarizado con los métodos como:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{ 
    //this will be called first. It will tell the table how many cells your table view will have to display 
    return [arrayToDisplay count]; 

} 

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { 

    //this is called after the count of rows is set. It will populate your table according to the data your array to display contains 

    [tableView setTarget:self]; 
    [tableView setAction:@selector(click)]; 

    NSString *identifier = [tableColumn identifier]; 

    if ([identifier isEqualToString:@"TheCell"]) { 

     CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self]; 
     cellView.cellText.stringValue = [arrayToDisplay objectAtIndex:row]; 
     return cellView; 
    } 

    return nil; 
} 

Y el método click que se activa cuando se selecciona una fila sería el siguiente:

-(void)click{ 

    int index = [table selectedRow]; 

    // Do something with your data 
    //e.g 
    [[arrayToDisplay objectAtIndex:index] findHiggsBoson]; 

} 

Y algo que debe agregarse a NSTableView:

NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"column"]; 
column.width = self.frame.size.width; 
[tableView addTableColumn:column]; 
1

No es necesario que la subclase NSTableView tenga subclases personalizadas NSTableViewCell. Se podría considerar el uso de un punto de vista basado en la Tabla Ver también ...

Cuestiones relacionadas