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

标签 cocoa nstableview

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

我想在基于 View 的 NSTableView 中的选定行上绘制自定义渐变,同时添加微妙的凸起效果。为此,我需要对所选行之前和之后的网格线使用较深的颜色(有关示例,请参阅 here)。我已经重写了 NSTableRowView 中的 drawSeparatorInRect: 方法来为所选行绘制自定义分隔线(使用 isSelected 方法作为标志),但是我不能对上面/下面的做同样的事情(因为我在底部/顶部画线)。

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

我尝试过的一些事情:

  1. 在所选行的 drawSeparatorInRect: 方法中,我尝试访问同级 View ([superview subviews]) 并强制上一个/下一个 View 再次绘制自己。
  2. NSTableView 子类中,当 selectedIndexes 更改时直接修改最近的行。
  3. 从其 drawSeparatorInRect: 方法中在所选行外部绘制线条,如 here 所示.

请注意,我这样做的方式是:一个行 View 询问是否选择了上一个/下一个,一个 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]];
}

此方法将(仅)绘制自己的底部分隔线。如果是选定的行,它将不使用默认颜色绘制该线,而是突出显示,然后它还会告诉前一行重新绘制其分隔线,即与选定行的顶部分隔线相同。

为了实现此功能,一旦选择移动,所选行上方的行需要重新绘制其底部分隔线。我通过在 NSTableView 委托(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;
}

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

关于cocoa - NSTableView:如何在选定行之前和之后绘制自定义分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8448371/

相关文章:

objective-c - nextKeyView 不起作用

objective-c - NStableview setDatasource EXC_BAD_ACCESS

cocoa - 在外部单击时,不要取消选择基于 View 的 NStableview 中的选定行

macos - 如何阻止 NSTableView 保存当前滚动位置?

iphone - 静态库的目的

cocoa : cocoa 中的哪个方法与 animateWithDuration:animations:completion: 相同?

cocoa - 以编程方式绑定(bind) NSTableView

Cocoa Core Data 和 Tab View - 如何判断 Tab View 何时完成 "loading",以便我可以告诉 View 自行初始化?

swift - 在 cocoa 应用程序中关闭单击时显示和隐藏窗口而不是终止应用程序

cocoa - NSTableview 中选定行的标签颜色