这是我的第一个问题,我会尽量说清楚。
我想在基于视图的NSTableView的选定行上绘制自定义渐变,同时添加微妙的凸起效果。为此,我需要为选定行前后的网格线使用较深的颜色(参见这里的示例)。我已经在NSTableRowView中覆盖了drawSeparatorInRect:
方法来为所选行绘制自定义分隔线(使用isSelected
方法作为标志),但我不能为上面/下面的一个做同样的事情(因为我在底部/顶部绘制线)。
我已经尝试了几种方法来告诉最近的行,它应该画一条较暗的分隔线,没有成功,因为显示步骤不遵循相同的顺序(我用drawSeparatorInRect:
中的NSLogs检查了它,似乎当你滚动一点这个顺序变化)。因此,有时(主要是在滚动之后)行不知道它应该使用较深的颜色,因为它在选中的行之前绘制自己(我认为在这一点上,被选中的行还不知道它被选中了,否则我不明白发生了什么)。
我尝试过的一些事情:
- 在选定行的
drawSeparatorInRect:
方法中,我试图访问兄弟姐妹视图([superview subviews]
)并强制前一个/下一个再次绘制自己。 - 从
NSTableView
子类中,当 selectedindex 改变时,直接修改最近的行。 - 从
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]];
}
这个方法将(只)绘制它自己的底部分隔线。如果它是被选中的行,它将不使用默认颜色绘制线条,而是高亮显示,然后它还将告诉前一行重新绘制其分隔线,即与被选中行的顶部分隔线相同。
为了使其工作,一旦选择移动,选中行上方的行需要重新绘制其底部分隔线。我通过在NSTableView委托中使用这个方法实现了这一点:
// 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;
}
此委托方法告诉仍然选中的行上方的行重新绘制其分隔线。