Pregunta

Hola Estoy tratando de utilizar un NSPopUpButtonCell dentro de un NSTableView. Básicamente, cuando se selecciona un elemento en la ventana emergente que quiero que aparece en la vista de tabla de columna / fila. Cuando se pulsa un elemento en la celda emergente lo guarde dentro de la fuente de datos utilizando "tableView: setObject: forTableColumn: fila", entonces cuando los solicita datos de la tabla I recuperar y y establecer el estado de la celda emergente en "tableView: objectValueForTableColumn: fila:". Se adjunta el código. Estoy completamente atascado en este momento. Espero que alguien pueda tener sentido de él. Gracias de antemano.

Esto es dentro del controlador:

  //Create the table columns
  NSTableColumn *nameColumn = [[NSTableColumn alloc] initWithIdentifier:LXDetailItemName];
  NSTableColumn *dataTypeColumn = [[NSTableColumn alloc] initWithIdentifier:LXDetailItemDataType];
  NSTableColumn *deviceColumn = [[NSTableColumn alloc] initWithIdentifier:LXDetailItemDevice];

  //Data type column drop down
  NSPopUpButtonCell *dataTypeDropDownCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:YES];
  [dataTypeDropDownCell setBordered:NO];
  [dataTypeDropDownCell setEditable:YES];

  NSArray *dataTypeNames = [NSArray arrayWithObjects:@"NULL", @"String", @"Money", @"Date", @"Int", nil];
  [dataTypeDropDownCell addItemsWithTitles:dataTypeNames];
  [dataTypeColumn setDataCell:dataTypeDropDownCell];

  //Add the columns to the table
  [tableView addTableColumn:nameColumn];
  [tableView addTableColumn:dataTypeColumn];
  [tableView addTableColumn:deviceColumn]; 
    enter code here

Esto está dentro de la clase de origen de datos / delegado.

enter code here

@implementation LXTestDataSource

- (id)init
{
 self = [super init];

 if (self)
 { 
  tableContents = [[NSMutableArray alloc] init];

  //Setup test data
  NSMutableArray *keys = [NSMutableArray arrayWithObjects:LXDetailItemName, LXDetailItemDataType, LXDetailItemDevice, nil];
  NSMutableArray *objects = [NSMutableArray arrayWithObjects:@"one", @"NULL", @"NULL", nil];

  for (int i = 0; i < 4; i++)
  {
   NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys];
   [tableContents addObject:dictionary];
   [dictionary release];
  }
 }

 return self;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
 return [tableContents count];
}


- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{ 
 if ([[aTableColumn identifier] isEqualToString:LXDetailItemDataType])
 {
  NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex];
  NSString *title = [rowDictionary objectForKey:LXDetailItemDataType];

  NSLog(@"objectValueForTableColumn: %@", title); //DEBUG

  return [NSNumber numberWithInt:[[aTableColumn dataCell] indexOfItemWithTitle:title]];
 }
 else if ([[aTableColumn identifier] isEqualToString:LXDetailItemDevice])
 {
  NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex];
  NSString *title = [rowDictionary objectForKey:LXDetailItemDevice];

  NSLog(@"objectValueForTableColumn: %@", title); //DEBUG

  return [NSNumber numberWithInt:[[aTableColumn dataCell] indexOfItemWithTitle:title]];
 }
 else
 {
  NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex];
  return [rowDictionary objectForKey:[aTableColumn identifier]]; 
 }
}


- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{ 
 if ([[aTableColumn identifier] isEqualToString:LXDetailItemDataType])
 {  
  NSMenuItem *menuItem = [[aTableColumn dataCell] itemAtIndex:[anObject integerValue]];

  NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex];

  NSLog(@"%@", [menuItem title]); //DEBUG

  //Update the object value at the column index
  [rowDictionary setObject:[menuItem title] forKey:LXDetailItemDataType];
 }
 else if ([[aTableColumn identifier] isEqualToString:LXDetailItemDevice])
 {
  NSMenuItem *menuItem = [[aTableColumn dataCell] itemAtIndex:[anObject integerValue]];

  NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex];

  NSLog(@"%@", [menuItem title]); //DEBUG

  //Update the object value at the column index
  [rowDictionary setObject:[menuItem title] forKey:LXDetailItemDevice]; 
 }
 else
 {
  //Get the row
  NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex];

  //Update the object value at the column index
  [rowDictionary setObject:anObject forKey:[aTableColumn identifier]];
 }
}

@end
¿Fue útil?

Solución

creo que he descubierto. Yo uso el "setTitle:" método de la celda aparecerá para actualizar el texto de la célula después de hacer clic en el elemento de menú. Aquí está el método vista delegado tabla que utilicé.

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
    if ([[aTableColumn identifier] isEqualToString:LXDetailItemDataType])
    {
        NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex];
        NSString *title = [rowDictionary objectForKey:LXDetailItemDataType];

        [aCell setTitle:title];
    }
    else if ([[aTableColumn identifier] isEqualToString:LXDetailItemDevice])
    {
        NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex];
        NSString *title = [rowDictionary objectForKey:LXDetailItemDevice];

        [aCell setTitle:title];
    }
}

Otros consejos

Creo que todo lo que debe necesitar para hacer después de establecer el valor en tableView:setObjectValue:forTableColumn:row: es llamar reloadData en la vista de tabla para forzarlo a que se actualice con los cambios realizados en el modelo de datos. NSPopUpButtonCell hace utilizar el índice de elemento como su valor objetivo, de manera que parte de las cosas debería funcionar correctamente sin que el código en el método delegado willDisplayCell.

Como acotación al margen, yo recomendaría el uso de la propiedad representedObject de un elemento de menú para decir la parte elementos en lugar del título. títulos de los elementos de menú potencialmente pueden ser localizados, y el uso de títulos de valores de datos de tienda es muy susceptible a la rotura.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top