0
votes

I've got a weird drawing bug happening. I have a view based NSTableView with simple rows or text cells. When I press the down arrow, selecting a row, all the text in the tableview shifts 1 pixel to the left, and then when I select the next row, it goes back. Very odd.

I pulled out any subclassing of rows or cells that I have and replace them with standard stuff, but it still happens.

Anyone seen this before?

1
I think it might be related to one of the columns being hidden.pizzafilms

1 Answers

0
votes

This can happen if you subclass "NSTableRowView" and use that to hi-light the "selected row".

This usually happens when there's a border to the left or to the right.

As per apples example called "TableViewPlaygound" (which can be found here: https://developer.apple.com/library/mac/samplecode/TableViewPlayground/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010727), this is what they use and I use it often for quick and dirty copy and paste hi-lighting:

- (void)drawSelectionInRect:(NSRect)dirtyRect {

        NSColor *primaryColor = [[NSColor alternateSelectedControlColor] colorWithAlphaComponent:0.4];
        NSColor *secondarySelectedControlColor = [[NSColor secondarySelectedControlColor] colorWithAlphaComponent:0.4];

    // Implement our own custom alpha drawing
    switch (self.selectionHighlightStyle) {
        case NSTableViewSelectionHighlightStyleRegular: {
            if (self.selected) {
                if (self.emphasized) {
                    [primaryColor set];
                } else {
                    [secondarySelectedControlColor set];
                }
                NSRect bounds = self.bounds;
                const NSRect *rects = NULL;
                NSInteger count = 0;
                [self getRectsBeingDrawn:&rects count:&count];
                for (NSInteger i = 0; i < count; i++) {
                    NSRect rect = NSIntersectionRect(bounds, rects[i]);
                    NSRectFillUsingOperation(rect, NSCompositeSourceOver);
                }
            }
            break;
        }
        default: {
            // Do super's drawing
            [super drawSelectionInRect:dirtyRect];
            break;
        }
    }
}

-(void)drawSeparatorInRect:(NSRect)dirtyRect
{
    NSRect sepRect = self.bounds;
    sepRect.origin.y = NSMaxY(sepRect) - 1;
    sepRect.size.height = 1;
    sepRect = NSIntersectionRect(sepRect, dirtyRect);
    if (!NSIsEmptyRect(sepRect)) {
        [[NSColor gridColor] set];
        NSRectFill(sepRect);
    }
}