Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa NSTableView:如何在选定行之前和之后绘制自定义分隔符_Cocoa_Nstableview - Fatal编程技术网

Cocoa NSTableView:如何在选定行之前和之后绘制自定义分隔符

Cocoa NSTableView:如何在选定行之前和之后绘制自定义分隔符,cocoa,nstableview,Cocoa,Nstableview,这是我在这里的第一个问题,我会尽可能清楚地回答 我想在基于视图的NSTableView中的选定行上绘制自定义渐变,同时添加微妙的凸起效果。为此,我需要为选定行前后的网格线使用较暗的颜色(请参见示例)。我已经在NSTableRowView中覆盖了drawSeparatorInRect:method来为所选行绘制自定义分隔线(使用isSelected方法作为标志),但我无法对上面/下面的行执行相同的操作(因为我在底部/顶部绘制了线) 我尝试了几种方法告诉最近的行它应该绘制一条较暗的分隔线,但没有成功

这是我在这里的第一个问题,我会尽可能清楚地回答

我想在基于视图的NSTableView中的选定行上绘制自定义渐变,同时添加微妙的凸起效果。为此,我需要为选定行前后的网格线使用较暗的颜色(请参见示例)。我已经在NSTableRowView中覆盖了
drawSeparatorInRect:
method来为所选行绘制自定义分隔线(使用
isSelected
方法作为标志),但我无法对上面/下面的行执行相同的操作(因为我在底部/顶部绘制了线)

我尝试了几种方法告诉最近的行它应该绘制一条较暗的分隔线,但没有成功,因为显示步骤的顺序不同(我在
drawsepositionrirect:
中使用NSLogs检查了它,似乎当您滚动一点时,此顺序会发生变化)。因此,有时(主要是在滚动之后)行不知道它应该使用较暗的颜色,因为它在所选行之前绘制自己(我认为在这一点上,所选行还没有意识到它已被选中,否则我不明白发生了什么)

我尝试过的一些事情:

  • 在所选行的
    drawSeparatorInRect:
    方法中,我尝试访问同级视图(
    [superview子视图]
    ),并强制上一个/下一个视图再次绘制自身
  • NSTableView
    子类中,当所选索引更改时,直接修改最近的行
  • 从所选行的
    drawSeparatorInRect:
    方法中绘制该行外部的线,如图所示
  • 请注意,我这样做是因为:一个行视图询问是否选择了上一个/下一个,一个closestRowIsSelected标志或外部调用一个方法“强制”深色

    我现在看到的是,所选行同时绘制顶部和底部边框,因此其中一个边框被放置在上一行/下一行中。。。这是微妙的,但它仍然存在

    任何帮助都会受到欢迎

    先谢谢你


    我没有发布任何代码,因为问题不存在(它只是用红色调用
    [NSBezierPath fillRect:rect]
    ),我想。。。因此,我没有什么要显示的。

    我也尝试过这个,并注意到drawSeparatorInRect:实际上只能绘制其底部分隔线,因为顶部分隔线(与前一行的底部分隔线相同)的位置在该行的clipRect外(上方)一个像素

    但是,我通过子类化NSTableRowView并使用drawSeparatorInRect:实现了它,如下所示:

    - (void)drawSeparatorInRect:(NSRect)dirtyRect
    {
        // Define our drawing colors
        NSColor *normalColor = [NSColor colorWithCalibratedWhite:0.76 alpha:1.0]; // Default separator color
        NSColor *selectedTopColor = [NSColor colorWithCalibratedWhite:0.60 alpha:1.0]; // Color of the top separator line of selected row
        NSColor *selectedBottomColor = [NSColor colorWithCalibratedWhite:0.60 alpha:1.0]; // Color of the bottom separator line of selected row
    
        // Define coordinates of separator line
        NSRect drawingRect = [self frame]; // Ignore dirtyRect
        drawingRect.origin.y = drawingRect.size.height - 1.0;
        drawingRect.size.height = 1.0; // Height of the separator line we're going to draw at the bottom of the row
    
        // Get the table view and info on row index numbers
        NSTableView *tableView = (NSTableView*)[self superview]; // The table view the row is part of
        NSInteger selectedRowNumber = [tableView selectedRow];
        NSInteger ownRowNumber = [tableView rowForView:self];
    
        // Set the color of the separator line
        [normalColor set]; // Default
        if (([self isSelected]) && ((selectedRowNumber + 1) < [tableView numberOfRows])) [selectedBottomColor set]; // If the row is selected, use selectedBottomColor
        if ((![self isSelected]) && (selectedRowNumber > 0) && (ownRowNumber == (selectedRowNumber-1))) [selectedTopColor set]; // If the row is followed by the selected row, draw its bottom separator line in selectedTopColor
    
        // Draw separator line
        NSRectFill (drawingRect);
    
        // If the row is selected, tell the preceding row to redraw its bottom separator line (which is also the top line of the selected row)
        if (([self isSelected]) && (selectedRowNumber > 0)) [tableView setNeedsDisplayInRect:[tableView rectOfRow:selectedRowNumber-1]];
    }
    

    此委托方法告诉仍选定行上方的行重新绘制其分隔线。在选择更改之前立即调用它。

    事实证明,Apple文档是正确的:NSTableRowView的默认功能是在行的底部而不是顶部绘制分隔线。我已经对上面的代码示例进行了相应的编辑。非常感谢,它确实做到了我想做的事情。但是,每当我选择一行或滚动tableview时,选中行的前一行会多次重新绘制自身,这会产生超粗体文本和其他奇怪的效果。我遗漏了什么吗?我用我的旧代码做的很好。但是,我将位于
    drawsepositionornrect:
    方法末尾的强制前一个单元格视图绘制自身的代码段移动到一个委托方法,该委托方法为我提供了新的选定行(我使用了
    tableView:shouldSelectRow:
    )。再次感谢您的帮助。我宁愿将
    setNeedsDisplayInRect:
    调用放在
    outlineViewSelectionDidChange:
    delegate方法中。
    // Tell the row above the row which is going to loose the selection to redraw its bottom separator line
    - (BOOL)selectionShouldChangeInTableView:(NSTableView *)aTableView
    {
        NSInteger selectedRowNumber = [aTableView selectedRow];
        if (selectedRowNumber > 0) {
        [aTableView setNeedsDisplayInRect:[aTableView rectOfRow:selectedRowNumber-1]];
        }
        return YES;
    }