2012-06-06 44 views
18

我有一个基于nstableview的视图。我想基于一些condtion为我所用下面在基于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;; 
} 

的委托方法被调用代码着色整行,但表似乎并没有被使用的委托方法返回NSTableRowView

这里的主要目的是根据一些条件着色整行。上面的实现有什么错误?

+0

要设置的backgroundColor,你需要使用' - (空)的tableView:(NSTableView的*)的tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger的)排在NSTableViewDelegate.'看我下面的更详细的解答。 – DPlusV

回答

0

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

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

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

注意在这个例子中它有它自己的标识符,所以记住要设置它,但是你也可以使用自动标识符。

我不知道是否可以直接链接到WWDC,但主页在这里:https://developer.apple.com/videos/wwdc/2011/,如果您搜索“基于视图的NSTableView Basic to Advanced”,您会发现它。这是值得关注的。

+0

我将使用绑定将数据填充到行 – sach

+0

是的..我想使用基于视图的 – sach

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

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

+3

这种方法是一种破解 - 它将单个单元格视图设置为图层托管(它并不总是您想要的),并且它不会更改整行的样式。请参阅我在此页面上的回答以获得有文件记录的方法 – DPlusV

36

对于其他人谁打这一点,并希望自定义NSTableRowView找到backgroundColor,有两种方法。

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

    例子:

    - (void)tableView:(NSTableView *)tableView 
        didAddRowView:(NSTableRowView *)rowView 
          forRow:(NSInteger)row { 
    
        rowView.backgroundColor = [NSColor redColor]; 
    
    } 
    
  2. 如果你确实需要自定义绘图,创建你自己NSTableRowView子与所需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; 
    } 
    
+0

如果您使用基于单元格的表格视图,这是否工作?我感觉它没有,但我只是想验证。 – Kyle

+1

你是我的时间保护者!感谢分享如何使用rowViewForRow委托。 – Tommy

+2

谢谢!这工作完美。在我发现这个之前,我对如何添加我的子类已经有点困惑,但是这可以创造奇迹。 –

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 

} 

不要忘记根据你的故事板来改变小区的标识,并在代码标识的“绿色”。当然,如果你想的话,背景颜色。