2012-06-06 43 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 

} 

不要忘記根據你的故事板來改變小區的標識,並在代碼標識的「綠色」。當然,如果你想的話,背景顏色。