18

我有一个基于视图的 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委托方法返回的表。

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

4

4 回答 4

39

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

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

    例子:

    - (void)tableView:(NSTableView *)tableView
        didAddRowView:(NSTableRowView *)rowView
               forRow:(NSInteger)row {
    
        rowView.backgroundColor = [NSColor redColor];
    
    }
    
  2. NSTableRowView如果您确实需要自定义绘图,请使用 desired创建自己的子类drawRect。然后,在 中实现以下内容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;
    }
    
于 2013-10-09T00:05:55.153 回答
1

最后它的工作如下

    - (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中找到

于 2012-06-07T03:32:48.480 回答
0

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

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

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

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

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

于 2012-06-06T08:47:01.333 回答
0

我重写了层方法。在斯威夫特 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

}

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

于 2017-04-26T14:32:59.547 回答