CocoaDev

Edit AllPages

I was looking for examples of how to set an NSTableView row’s background color based on a value in the row. I didn’t find anything but came up with a method I thought I’d share. I subclassed NSTableView and overrode drawBackgroundInClipRect:. It gets the background color from the delegate through a call to tableView:backgroundColorForRow:. To leave the background color at the default, the delegate simply returns nil.

Here’s the header (including a protocol that the delegate can conform to):

@interface RowColorTableView : NSTableView { BOOL delegateRespondsToBackgroundColorForRow; }

@end

@protocol RowColorTableViewDelegate

and the implementation:

#import “RowColorTableView.h”

@implementation RowColorTableView

@end

-SteveNicholson