Pedro Vieira
Pedro Vieira

Reputation: 3370

How to make round cornered NSTableView rows?

I'd like to make something like this (on the new iTunes):
enter image description here

As you can see when the row is selected it's "highlight" state is a round cornered row.
How can I achieve something like this?

Upvotes: 1

Views: 1026

Answers (1)

Justin Boo
Justin Boo

Reputation: 10198

You need to subclass NSTableview and override -highlightSelectionInClipRect: method.

It can be done like this:

Change your tableView's highlighting mode from regular to Source list in Attributes Inspector:

Change highlight mode image

And now subclass NSTableView like this:

-(void)highlightSelectionInClipRect:(NSRect)theClipRect
{
    NSRange visibleRowIndexes = [self rowsInRect:theClipRect];
    NSIndexSet *selectedRowIndexes = [self selectedRowIndexes];
    NSUInteger endRow = visibleRowIndexes.location + visibleRowIndexes.length;
    NSUInteger row;

    for (row=visibleRowIndexes.location; row<endRow; row++)
    {
        if([selectedRowIndexes containsIndex:row])
        {
            NSRect rowRect = NSInsetRect([self rectOfRow:row], 3, 4);
            NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rowRect xRadius:4.0 yRadius:4.0];
            [[NSColor colorWithCalibratedRed:0.474 green:0.588 blue:0.743 alpha:1] set];
            [path fill];
        }
    }
}

Result:

Result image

Upvotes: 2

Related Questions