我将表的列子类化为 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];
}
尝试:
[cell contentView] setBackgroundColor:[NSColor redColor]];