Question

Comment puis-je changer la couleur d'une cellule dans mon NSTableView?

Était-ce utile?

La solution

Essayez d'utiliser un NSView personnalisé pour cela, ou la méthode de NSTableView de -setBackgroundColor:.

Autres conseils

Dans votre NSTableViewDelegate pour la NSTableView, mettre en œuvre cette méthode:

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

Le NSTableView appelle cela son délégué avant d'afficher chaque cellule afin que vous puissiez affecter son apparence. En supposant que vous utilisez NSTextFieldCells, pour la cellule que vous souhaitez modifier appel:

[cell setBackgroundColor:...];

Ou, si vous voulez changer la couleur du texte:

[cell setTextColor:...];

Si vous voulez des colonnes d'avoir des apparences différentes, ou si toutes les colonnes ne sont pas NSTextFieldCells, utilisez [tableColumn identifier] pour, heu, identifier la colonne. Vous pouvez définir l'identifiant dans Interface Builder en sélectionnant la colonne du tableau.

// TESTE - Swift 3 solution ... pour changer la couleur du texte des cellules dans une seule colonne. Toutes les colonnes de mon tableview ont un identifiant unique

    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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top