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?
+1
A:
There is no settable property for this, however you can respond to the delegate method -tableView:willDisplayCell:forTableColumn:row:
and set the cell's background color based on the evenness of the row number.
Joshua Nozzi
2010-10-20 00:19:36
This doesn't seem to work when there are no cells in the table view (doesn't change the background of the table view itself)
macatomy
2010-10-20 04:07:01
A:
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
Ken Aspeslagh
2010-10-20 00:59:25
You want to change the white color too? What are you doing this for exactly?
Ken Aspeslagh
2010-10-20 18:39:35
@Ken I think its a fairly common practice to customize the table view cell colors, surprised that NSTableView doesn't support this by default. The controlAlternatingRowBackgroundColors method worked great, I think its the only way to do this.
macatomy
2010-10-23 21:30:34
The only down-side is that it will affect all such tableviews in the app.
Ken Aspeslagh
2010-10-24 01:20:46
+1
A:
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.
macatomy
2010-10-26 02:46:57
+100 I came across this question a few days ago hoping for a good answer, but didn't find a satisfactory one. Thankfully, I happened to see it come up again! Thanks!
Dave DeLong
2010-10-26 05:17:12