Domanda

Come posso cambiare il colore di una cellula del mio NSTableView?

È stato utile?

Soluzione

Provare a usare un NSView personalizzato per questo, o il metodo di NSTableView -setBackgroundColor:.

Altri suggerimenti

Nel vostro NSTableViewDelegate per il NSTableView, implementare questo metodo:

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

Il NSTableView chiama questo il suo delegato prima di visualizzare ogni cella in modo da poter influenzare il suo aspetto. Supponendo che si sta utilizzando NSTextFieldCells, per la cella che si desidera chiamare il cambiamento:

[cell setBackgroundColor:...];

In alternativa, se si desidera modificare il colore del testo:

[cell setTextColor:...];

Se si desidera colonne per avere diversi aspetti, o se tutte le colonne non sono NSTextFieldCells, uso [tableColumn identifier] a, ehm, identificare la colonna. È possibile impostare l'identificatore in Interface Builder selezionando la colonna della tabella.

// ZERO - Swift 3 soluzione ... per cambiare colore del testo della cella in una singola colonna. Tutte le colonne nella mia Tableview hanno un identificatore univoco

    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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top