【问题标题】:Coloring rows in View based NSTableview基于视图的 NSTableview 中的着色行
【发布时间】:2012-06-10 06:23:01
【问题描述】:

我有一个基于视图的 nstableview。我想根据我在下面使用代码的某些条件为整行着色

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row 
{
    NSTableRowView *view = [[NSTableRowView alloc] initWithFrame:NSMakeRect(1, 1, 100, 50)];

    [view setBackgroundColor:[NSColor redColor]];
    return view;;
}

委托方法被调用,但表似乎没有使用委托方法返回的NSTableRowView

这里的主要目的是根据某些条件为整行着色。上面的实现有什么问题?

【问题讨论】:

  • 要设置backgroundColor,需要使用- (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row in your NSTableViewDelegate.下面看我更详细的回答。

标签: cocoa nstableview


【解决方案1】:

对于遇到此问题并想要自定义 NSTableRowView 背景颜色的其他人,有两种方法。

  1. 如果您不需要自定义绘图,只需在您的- (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row 中设置rowView.backgroundColor NSTableViewDelegate

    例子:

    - (void)tableView:(NSTableView *)tableView
        didAddRowView:(NSTableRowView *)rowView
               forRow:(NSInteger)row {
    
        rowView.backgroundColor = [NSColor redColor];
    
    }
    
  2. 如果您确实需要自定义绘图,请使用所需的drawRect 创建您自己的NSTableRowView 子类。然后,在NSTableViewDelegate 中实现以下内容:

    例子:

    - (NSTableRowView *)tableView:(NSTableView *)tableView
                    rowViewForRow:(NSInteger)row {
        static NSString* const kRowIdentifier = @"RowView";
        MyRowViewSubclass* rowView = [tableView makeViewWithIdentifier:kRowIdentifier owner:self];
        if (!rowView) {
            // Size doesn't matter, the table will set it
            rowView = [[[MyRowViewSubclass alloc] initWithFrame:NSZeroRect] autorelease];
    
            // This seemingly magical line enables your view to be found
            // next time "makeViewWithIdentifier" is called.
            rowView.identifier = kRowIdentifier; 
        }
    
        // Can customize properties here. Note that customizing
        // 'backgroundColor' isn't going to work at this point since the table
        // will reset it later. Use 'didAddRow' to customize if desired.
    
        return rowView;
    }
    

【讨论】:

  • 如果您使用的是基于单元格的表格视图,这是否有效?我的印象是它没有,但我只是想验证一下。
  • 你是我的时间节省者!感谢分享如何使用 rowViewForRow 委托。
  • 谢谢!这非常有效。在我发现这个之前,我对如何添加我的子类行有点困惑,但这很有效。
  • 这是最好的解决方案
  • 当我这样做时,所有单元格视图的子视图都使用旧背景颜色绘制,而我设置的颜色仅在它们周围绘制。
【解决方案2】:

最后它工作如下

    - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row

{
        NSView *cellView = (NSView*) [tableView makeViewWithIdentifier:[tableColumn identifier] owner:[tableView delegate]];
        CALayer *viewLayer = [CALayer layer];
        [viewLayer setBackgroundColor:[[NSColor redcolor] CGColor]];
        [cellView setWantsLayer:YES]; 
        [cellView setLayer:viewLayer];
        return cellView;
    }

请注意..您需要将 nscolor 转换为 cgcolor,您可以在 https://gist.github.com/707921http://forrst.com/posts/CGColor_Additions_for_NSColor-1eW 中找到它

【讨论】:

  • 这种方法是一种技巧——它将单个单元格视图设置为层托管(这并不总是您想要的),并且它不会改变整个行的样式。有关记录方法,请参阅我在此页面上的答案。
【解决方案3】:

如果您观看 WWDC 2011 上关于基于视图的表格视图的演示,您会发现主要思想是在 Interface Builder 中创建视图,然后从那里获取它们。比如:

[tableView makeViewWithIdentifier:@"GroupRow" owner:self];

获得视图后,只需设置其属性并返回即可。

请注意,在此示例中它有自己的标识符,因此请记住设置它,但您也可以使用自动标识符。

我不知道直接链接到 WWDC 是否有效,但主页在这里:https://developer.apple.com/videos/wwdc/2011/,如果您搜索“基于视图的 NSTableView Basic to Advanced”,您会找到它。非常值得一看。

【讨论】:

  • 我将使用绑定将数据填充到行中
  • 是的..我想使用基于视图的
【解决方案4】:

我重新编写了层方法。 在 Swift 3.2 中

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

    let greenCell = self.tableview.make(withIdentifier: "green", owner: self)
    let layer:CALayer = CALayer()
    layer.backgroundColor = NSColor.green.cgColor

    greenCell?.wantsLayer = true
    greenCell?.layer = layer

    return greenCell

}

不要忘记根据您的情节提要更改单元格的标识符,并在代码标识符“绿色”中。当然,如果您愿意,还可以使用背景颜色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多