【问题标题】:Draw NSTableView Alternating Rows Like iTunes 11像 iTunes 11 一样绘制 NSTableView 交替行
【发布时间】:2013-01-06 01:29:54
【问题描述】:

我知道关于更改交替行颜色还有其他问题。这很容易,这不是我想做的。

我想在基于视图的 NSTableView 中绘制自定义的交替颜色行,这些行看起来像 iTunes 11 中的那些(行的顶部和底部有轻微的边框,如此屏幕截图所示):

注意:

我知道我可以继承 NSTableRowView 并在那里进行自定义绘图。但是,这不是一个可接受的答案,因为自定义行将仅用于表中有数据的行。换句话说,如果表格只有 5 行,那 5 行将使用我的自定义 NSTableRowView 类,但表格其余部分(为空)中的剩余“行”将使用标准交替颜色。在这种情况下,前 5 行将显示挡板,其余的不会。不好。

那么,我如何破解 NSTableView 来为填充行和空行绘制这些样式交替的行?

【问题讨论】:

  • 试图将其作为indragie's question 的骗子而关闭,仅仅是因为他的解决方案应该间接解决您的问题。
  • 我在写我的之前看到了那篇文章。问题是他想对交替的行使用纯色,这很容易。我想为交替行做自定义绘图,这是一个完全不同的问题。我不能使用他的方法,因为我需要绘制边框;不只是用交替的 NSColors 填充行。
  • 该问题中使用的技巧涉及选择颜色和填充矩形(更重要的是,找到正确的方法覆盖来完成它)。但是,一旦您在一个可以绘制填充矩形的地方,是什么阻止您在该矩形的顶部绘制一条白线并在底部绘制一条灰线?

标签: objective-c macos cocoa nstableview


【解决方案1】:

正如您所说的那样,“小边框”实际上可以通过我们的一点作弊轻松完成。因为,如果仔细观察,每个单元格的顶部都是比深色交替行略浅的蓝色,每个单元格的底部都是深灰色,所以可以继承 NSTableView,然后覆盖- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect:

- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect
{
    //Use the drawing code from http://stackoverflow.com/a/5101923/945847, but change the colors to
    //look like iTunes's alternating rows.
    NSRect cellBounds = [self rectOfRow:row];
    NSColor *color = (row % 2) ? [NSColor colorWithCalibratedWhite:0.975 alpha:1.000] : [NSColor colorWithCalibratedRed:0.932 green:0.946 blue:0.960 alpha:1.000];
    [color setFill];
    NSRectFill(cellBounds);

    /* Slightly dark gray color */
    [[NSColor colorWithCalibratedWhite:0.912 alpha:1.000] set];
    /* Get the current graphics context */
    CGContextRef currentContext = [[NSGraphicsContext currentContext]graphicsPort];
    /*Draw a one pixel line of the slightly lighter blue color */
    CGContextSetLineWidth(currentContext,1.0f);
    /* Start the line at the top of our cell*/
    CGContextMoveToPoint(currentContext,0.0f, NSMaxY(cellBounds));
    /* End the line at the edge of our tableview, for multi-columns, this will actually be overkill*/
    CGContextAddLineToPoint(currentContext,NSMaxX(cellBounds), NSMaxY(cellBounds));
    /* Use the context's current color to draw the line */
    CGContextStrokePath(currentContext);

    /* Slightly lighter blue color */
    [[NSColor colorWithCalibratedRed:0.961 green:0.970 blue:0.985 alpha:1.000] set];
    CGContextSetLineWidth(currentContext,1.0f);
    CGContextMoveToPoint(currentContext,0.0f,1.0f);
    CGContextAddLineToPoint(currentContext,NSMaxX(self.bounds), 1.0f);
    CGContextStrokePath(currentContext);

    [super drawRow:row clipRect:clipRect];
}

当在一个快速的小表格视图中完成时,看起来像这样:

但是tableview的顶部和底部怎么办呢?毕竟,它们要么是丑陋的白色,要么是默认的交替行颜色。好吧,正如 Apple 所揭示的(在一个标题为 View Based NSTableView, Basic To Advanced 的演讲中),你可以覆盖 -(void)drawBackgroundInClipRect:(NSRect)clipRect 并做一些数学运算来绘制 tableview 的背景,比如额外的行。快速实现如下所示:

-(void)drawBackgroundInClipRect:(NSRect)clipRect
{
    // The super class implementation obviously does something more
    // than just drawing the striped background, because
    // if you leave this out it looks funny
    [super drawBackgroundInClipRect:clipRect];

    CGFloat   yStart   = 0;
    NSInteger rowIndex = -1;

    if (clipRect.origin.y < 0) {
        while (yStart > NSMinY(clipRect)) {
            CGFloat yRowTop = yStart - self.rowHeight;

            NSRect rowFrame = NSMakeRect(0, yRowTop, clipRect.size.width, self.rowHeight);
            NSUInteger colorIndex = rowIndex % self.colors.count;
            NSColor *color = [self.colors objectAtIndex:colorIndex];
            [color set];
            NSRectFill(rowFrame);

            /* Slightly dark gray color */
            [[NSColor colorWithCalibratedWhite:0.912 alpha:1.000] set];
            /* Get the current graphics context */
            CGContextRef currentContext = [[NSGraphicsContext currentContext]graphicsPort];
            /*Draw a one pixel line of the slightly lighter blue color */
            CGContextSetLineWidth(currentContext,1.0f);
            /* Start the line at the top of our cell*/
            CGContextMoveToPoint(currentContext,0.0f, yRowTop + self.rowHeight - 1);
            /* End the line at the edge of our tableview, for multi-columns, this will actually be overkill*/
            CGContextAddLineToPoint(currentContext,NSMaxX(clipRect), yRowTop + self.rowHeight - 1);
            /* Use the context's current color to draw the line */
            CGContextStrokePath(currentContext);

            /* Slightly lighter blue color */
            [[NSColor colorWithCalibratedRed:0.961 green:0.970 blue:0.985 alpha:1.000] set];
            CGContextSetLineWidth(currentContext,1.0f);
            CGContextMoveToPoint(currentContext,0.0f,yRowTop);
            CGContextAddLineToPoint(currentContext,NSMaxX(clipRect), yRowTop);
            CGContextStrokePath(currentContext);

            yStart -= self.rowHeight;
            rowIndex--;
        }
    }
}

但是,这会使 tableview 的底部留下同样难看的空白白色!所以,我们还必须覆盖-(void)drawGridInClipRect:(NSRect)clipRect。另一个快速实现如下所示:

-(void)drawGridInClipRect:(NSRect)clipRect {
    [super drawGridInClipRect:clipRect];

    NSUInteger numberOfRows = self.numberOfRows;
    CGFloat yStart = 0;
    if (numberOfRows > 0) {
        yStart = NSMaxY([self rectOfRow:numberOfRows - 1]);
    }
    NSInteger rowIndex = numberOfRows + 1;

    while (yStart < NSMaxY(clipRect)) {
        CGFloat yRowTop = yStart - self.rowHeight;

        NSRect rowFrame = NSMakeRect(0, yRowTop, clipRect.size.width, self.rowHeight);
        NSUInteger colorIndex = rowIndex % self.colors.count;
        NSColor *color = [self.colors objectAtIndex:colorIndex];
        [color set];
        NSRectFill(rowFrame);

        /* Slightly dark gray color */
        [[NSColor colorWithCalibratedWhite:0.912 alpha:1.000] set];
        /* Get the current graphics context */
        CGContextRef currentContext = [[NSGraphicsContext currentContext]graphicsPort];
        /*Draw a one pixel line of the slightly lighter blue color */
        CGContextSetLineWidth(currentContext,1.0f);
        /* Start the line at the top of our cell*/
        CGContextMoveToPoint(currentContext,0.0f, yRowTop - self.rowHeight);
        /* End the line at the edge of our tableview, for multi-columns, this will actually be overkill*/
        CGContextAddLineToPoint(currentContext,NSMaxX(clipRect), yRowTop - self.rowHeight);
        /* Use the context's current color to draw the line */
        CGContextStrokePath(currentContext);

        /* Slightly lighter blue color */
        [[NSColor colorWithCalibratedRed:0.961 green:0.970 blue:0.985 alpha:1.000] set];
        CGContextSetLineWidth(currentContext,1.0f);
        CGContextMoveToPoint(currentContext,0.0f,yRowTop);
        CGContextAddLineToPoint(currentContext,NSMaxX(self.bounds), yRowTop);
        CGContextStrokePath(currentContext);

        yStart += self.rowHeight;
        rowIndex++;
    }
}

当一切都说完后,我们在剪辑视图的顶部和底部得到了漂亮的小假表视图单元格行,看起来有点像这样:

完整的子类可以在here找到。

【讨论】:

  • 太棒了!感谢您的详细回复!
  • 嗯,试了之后,就close了。但是,当有问题的 tableView 是 NSOutlineView 时,在树中展开/折叠项目时,绘图会全部搞砸。这显然与操作 clipRects 有关。我会坚持下去,看看我是否可以调整一些东西来解决这个问题。
  • 您没有指定。我必须重做大纲视图的绘图代码。基本上是 TableView 或什么都没有。
  • CodaFi 的解决方案很接近,但并不准确。一些自鸣得意的胡须关闭了这个线程,所以如果将来有人在寻找如何做到这一点,我已经制定了一个防弹方法,如果你联系我,我会分享。 (我会在这里发布,但线程显然已关闭。)
  • @Bryan - 你做过要点吗?我真的很想看看。
【解决方案2】:

你可以使用

- (void)setUsesAlternatingRowBackgroundColors:(BOOL)useAlternatingRowColors

使用useAlternatingRowColors YES 指定背景的标准交替行颜色,NO 指定纯色。

【讨论】:

  • 这是正确答案。没有必要对任何东西进行子类化。它已经由原始的NSTableView 类提供。您可以按照上面所说的进行编码,也可以在 Interface Builder 中进行:检查 NSTableView 中的“交替行”,而不是表视图所在的滚动视图。
【解决方案3】:

我发现您可以在drawBackgroundInClipRect 中绘制顶部和底部部分——本质上是在@CodaFi 解决方案中缺少的else 子句中。

假设您可以访问 backgroundColor 和 alternateBackgroundColor,这是 Swift 中的一种方法:

override func drawBackground(inClipRect clipRect: NSRect) {

    // I didn't find leaving this out changed appearance at all unlike
    // CodaFi stated.
    super.drawBackground(inClipRect: clipRect)

    guard usesAlternatingRowBackgroundColors else { return }

    drawTopAlternatingBackground(inClipRect: clipRect)
    drawBottomAlternatingBackground(inClipRect: clipRect)
}

fileprivate func drawTopAlternatingBackground(inClipRect clipRect: NSRect) {

    guard clipRect.origin.y < 0 else { return }

    let backgroundColor = self.backgroundColor
    let alternateColor = self.alternateBackgroundColor

    let rectHeight = rowHeight + intercellSpacing.height
    let minY = NSMinY(clipRect)
    var row = 0

    while true {

        if row % 2 == 0 {
            backgroundColor.setFill()
        } else {
            alternateColor.setFill()
        }

        let rowRect = NSRect(
            x: 0,
            y: (rectHeight * CGFloat(row) - rectHeight),
            width: NSMaxX(clipRect),
            height: rectHeight)
        NSRectFill(rowRect)
        drawBezel(inRect: rowRect)

        if rowRect.origin.y < minY { break }

        row -= 1
    }
}

fileprivate func drawBottomAlternatingBackground(inClipRect clipRect: NSRect) {

    let backgroundColor = self.backgroundColor
    let alternateColor = self.alternateBackgroundColor

    let rectHeight = rowHeight + intercellSpacing.height
    let maxY = NSMaxY(clipRect)
    var row = rows(in: clipRect).location

    while true {

        if row % 2 == 1 {
            backgroundColor.setFill()
        } else {
            alternateColor.setFill()
        }

        let rowRect = NSRect(
            x: 0,
            y: (rectHeight * CGFloat(row)),
            width: NSMaxX(clipRect),
            height: rectHeight)
        NSRectFill(rowRect)
        drawBezel(inRect: rowRect)

        if rowRect.origin.y > maxY { break }

        row += 1
    }
}

func drawBezel(inRect rect: NSRect) {

    let topLine = NSRect(x: 0, y: NSMaxY(rect) - 1, width: NSWidth(rect), height: 1)
    NSColor(calibratedWhite: 0.912, alpha: 1).set()
    NSRectFill(topLine)

    let bottomLine = NSRect(x: 0, y: NSMinY(rect)   , width: NSWidth(rect), height: 1)
    NSColor(calibratedRed:0.961, green:0.970, blue:0.985, alpha:1).set()
    NSRectFill(bottomLine)
}

如果你不画 NSTableRowView 子类:

override func drawRow(_ row: Int, clipRect: NSRect) {

    let rowRect = rect(ofRow: row)
    let color = row % 2 == 0 ? self.backgroundColor : self.alternateBackgroundColor
    color.setFill()
    NSRectFill(rowRect)
    drawBezel(inRect: rowRect)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    相关资源
    最近更新 更多