Change color of NSTableViewCell

后端 未结 4 469
粉色の甜心
粉色の甜心 2021-01-17 17:08

How can I change the color of a cell in my NSTableView?

4条回答
  •  灰色年华
    2021-01-17 17:27

    //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;
    }
    

提交回复
热议问题