2013年3月26日火曜日

Customizing selection color for cell-based NSTableView

I would like to show you about the way to make custom selection color on cell-based NSTableView.

1. Make subclass of NSTableView
2. Set the highlight style as NSTableViewSelectionHighlightStyleNone to the subclass like this.
[self setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];

3. Override following method in the subclass and set background color for each case.
exmaple:

- (void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect
{
    NSInteger selectedRowIndex = [self selectedRow];
    [[NSColor colorWithCalibratedWhite:(rowIndex==selectedRowIndex) ? 0.6 : 0.9 alpha:1.0] setFill];
    NSRectFill([self rectOfRow:rowIndex]);
    [super drawRow:rowIndex clipRect:clipRect];
}

0 件のコメント: