简体   繁体   中英

Can't change color of column that i subclass it

I subclass my table's column to NStextFieldCell . When I want to change column's color with below given code, color doesn't change. Why?

- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    if(row%2==0)
        [cell setBackgroundColor:[NSColor knobColor]];
    else
        [cell setBackgroundColor:[NSColor lightGrayColor]];

    [cell setDrawsBackground: YES];
}

I use this code in NSTextFieldCell subclass

- (NSRect)titleRectForBounds:(NSRect)theRect 
{
    NSRect titleFrame = [super titleRectForBounds:theRect];
    NSSize titleSize = [[self attributedStringValue] size];
    titleFrame.origin.y = theRect.origin.y + (theRect.size.height - titleSize.height) / 2.0;
    return titleFrame;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 
{
    NSRect titleRect = [self titleRectForBounds:cellFrame];
    [[self attributedStringValue] drawInRect:titleRect];
}

尝试:

[cell contentView] setBackgroundColor:[NSColor redColor]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM