gpt4 book ai didi

cocoa - 基于 NSTableview 的 View 中的行着色

转载 作者:行者123 更新时间:2023-12-03 16:02:06 25 4
gpt4 key购买 nike

我有一个基于 nstableview 的 View 。我想根据我使用下面代码的某些条件对整行进行着色

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

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

调用了委托(delegate)方法,但表格似乎没有使用委托(delegate)方法返回的 NSTableRowView

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

最佳答案

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

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

    示例:

    - (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;
    }

关于cocoa - 基于 NSTableview 的 View 中的行着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10910779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com