I'm using the "Alternating Rows" option in Interface Builder to get alternating row colors on an NSTableView. Is there any way to change the colors of the alternating rows?
Go to Format > Shading and configure your alternating colors in the "Row Banding" and "Column Banding" sections. You can adjust the colors as well as how many rows to include before alternating (band size) and at what level in your table the banding should be applied.
If you want to use an undocumented way, make a NSColor
category and override _blueAlternatingRowColor
like this:
@implementation NSColor (ColorChangingFun)
+(NSColor*)_blueAlternatingRowColor
{
return [NSColor redColor];
}
@end
or to change both colors, override controlAlternatingRowBackgroundColors
to return an array of colors you want alternated.
@implementation NSColor (ColorChangingFun)
+(NSArray*)controlAlternatingRowBackgroundColors
{
return [NSArray arrayWithObjects:[NSColor redColor], [NSColor greenColor], nil];
}
@end
Found a better way to do it here. That method overrides the highlightSelectionInClipRect: method in an NSTableView subclass so you can use any color you want for the alternating rows. It's not as hackish as using an NSColor category, and it only affects table views you choose.
I subclassed NSTableView and implemented drawRow:clipRect: like this...
- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect
{
NSColor *color = (row % 2) ? [NSColor redColor] : [NSColor whiteColor];
[color setFill];
NSRectFill([self rectOfRow:row]);
[super drawRow:row clipRect:clipRect];
}
It seems to work, but it's so simple that I'm wondering if I'm missing something.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With