【问题标题】:NSTableView odd display bug when selection row changes选择行更改时 NSTableView 奇怪的显示错误
【发布时间】:2015-04-10 21:48:26
【问题描述】:

我遇到了一个奇怪的绘图错误。我有一个基于视图的 NSTableView,其中包含简单的行或文本单元格。当我按下向下箭头,选择一行时,tableview 中的所有文本向左移动 1 个像素,然后当我选择下一行时,它会返回。很奇怪。

我提取了我拥有的行或单元格的任何子类,并用标准的东西替换它们,但它仍然会发生。

有人见过这个吗?

【问题讨论】:

  • 我认为这可能与被隐藏的列之一有关。

标签: cocoa nstableview


【解决方案1】:

如果您将“NSTableRowView”子类化并使用它来高亮“选定行”,则会发生这种情况。

这通常发生在左侧或右侧有边框时。

根据名为“TableViewPlaygound”的苹果示例(可在此处找到:https://developer.apple.com/library/mac/samplecode/TableViewPlayground/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010727),这是他们使用的,我经常使用它来进行快速而肮脏的复制和粘贴高亮显示:

- (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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 2017-03-24
    • 2016-04-21
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    相关资源
    最近更新 更多