Pregunta

¿Cómo puedo cambiar el color de una celda en mi NSTableView?

¿Fue útil?

Solución

Trate de usar un NSView personalizado para que, o método de NSTableView -setBackgroundColor:.

Otros consejos

En su NSTableViewDelegate para la NSTableView, poner en práctica este método:

- (void)tableView:(NSTableView *)tableView 
  willDisplayCell:(id)cell 
   forTableColumn:(NSTableColumn *)tableColumn 
              row:(NSInteger)row

El NSTableView llama a esto en su delegado antes de mostrar cada célula por lo que puede afectar su apariencia. Suponiendo que está usando NSTextFieldCells, para la celda que desea cambiar la palabra:

[cell setBackgroundColor:...];

O, si desea cambiar el color del texto:

[cell setTextColor:...];

Si quieres columnas que tienen diferentes apariencias, o si todas las columnas no son NSTextFieldCells, el uso [tableColumn identifier] a, er, identificar la columna. Se puede establecer el identificador en el Interface Builder, seleccionando la columna de la tabla.

// PROBADO - solución Swift 3 ... para cambiar el color del texto de la celda en una sola columna. Todas las columnas de mi tableview tienen un identificador único

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

        let myCell:NSTableCellView = tableView.make(withIdentifier: (tableColumn?.identifier)!, owner: self) as! NSTableCellView
        if tableColumn?.identifier == "MyColumn" {
            let results = arrayController.arrangedObjects as! [ProjectData]
            let result = results[row]
            if result.ebit < 0.0 {
                myCell.textField?.textColor = NSColor.red
            } else {
                myCell.textField?.textColor = NSColor.black
            }
        }
        return myCell
    }
//for VIEW based TableViews using Objective C
//in your NSTableViewDelegate, implement the following
//this customization makes the column numbers red if negative.
- (NSView *)tableView:(NSTableView *)inTableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                  row:(NSInteger)row
{
    NSTableCellView *result = nil;
    if ([tableColumn.title isEqualToString: @"Amount"]) {
        //pick one of the following methods to identify the NSTableCellView
        //in .xib file creation, leave identifier "blank" (default)
         result = [inTableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
        //or set the Amount column's NSTableCellView's identifier to "Amount"
        result = [inTableView makeViewWithIdentifier:@"Amount" owner:self];
        id aRecord = [[arrayController arrangedObjects] objectAtIndex:row];
        //test the relevant field's value
        if ( aRecord.amount < 0.0 )
            [[result textField] setTextColor:[NSColor colorWithSRGBRed:1.0 green:0.0 blue:0.0 alpha:1.0]];
    } else {
        //allow the defaults to handle the rest of the columns
        result = [inTableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
    }
    return result;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top