【问题标题】:Custom background in view-based NSTableView基于视图的 NSTableView 中的自定义背景
【发布时间】:2011-12-28 22:26:32
【问题描述】:

通过覆盖NSTableRowView-drawBackgroundInRect:,我在基于视图的NSTableView 中自定义了交替行颜色。这在很大程度上是因为单元格本身的颜色会发生变化,但它显然不会影响表格视图本身的背景(例如,当滚动视图被反弹时)。截图:

自定义它的最佳方式是什么?我之前posted a question 也遇到过同样的问题,但使用的是基于单元格的表格视图。我找到的解决方案似乎不适用于基于视图的表格视图。

【问题讨论】:

  • 你也想要背景交替吗?
  • 是的,使用与单元格相同的颜色
  • 你为什么不尝试复制drawBackgroundInClipRect中的空行? NSTableView 也是用这个方法画的,好像不难。

标签: objective-c cocoa nstableview nsscrollview


【解决方案1】:

您是否尝试过覆盖 NSTableView 的 drawBackgroundInClipRect:(NSRect)clipRect

【讨论】:

    【解决方案2】:

    有一个演讲视频“基于视图的 NSTableView 基础到高级”(here 提供),其中最后一行下方的背景被绘制。

    为了扩展该技术,您可以创建 NSTableView 的子类并添加一些代码:

    // somewhere in your setup code (colors just intended as examples):
    
    tableView.colors = [NSArray arrayWithObjects: [NSColor lightGrayColor],[NSColor grayColor], nil]; 
    
    
    // In the table view subclass:
    
    -(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];
    
        NSRect boundsToDraw = clipRect;
    
        CGFloat   yStart   = 0;
        NSInteger rowIndex = -1;
    
        if ( clipRect.origin.y < 0 ) {        
    
            while (yStart > NSMinY(boundsToDraw)) {
    
                CGFloat yRowTop = yStart - self.rowHeight;
    
                NSRect rowFrame = NSMakeRect(0, yRowTop, boundsToDraw.size.width, self.rowHeight);
    
                NSUInteger colorIndex = rowIndex % self.colors.count;
    
                NSColor *color = [self.colors objectAtIndex:colorIndex];
    
                [color set];
    
                NSRectFill(rowFrame);
    
                yStart -= self.rowHeight;
    
                rowIndex--;
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我没有尝试过,但是如果你只是覆盖 -drawRect: 会发生什么?

      【讨论】:

      • -drawRect: 并不意味着在 NSTableView 子类中被覆盖,这可能会产生不需要的结果。
      猜你喜欢
      • 2015-03-10
      • 2011-12-18
      • 2019-07-05
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多