无法更改我将其子类化的列的颜色

时间:2012-04-22 19:13:31

标签: objective-c cocoa nstableview nstextfieldcell

我将表格的列子类化为NStextFieldCell。当我想用下面给出的代码更改列的颜色时,颜色不会改变。为什么呢?

- (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];
}

我在NSTextFieldCell子类

中使用此代码
- (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];
}

1 个答案:

答案 0 :(得分:1)

尝试:

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