簡體   English   中英

如何更改NSOutlineView的每個單元格的顏色或NSTableCellView

[英]How do I change the color or a NSTableCellView for each cell of an NSOutlineView

我已經看了很多問題,但找不到基於視圖的NSOutlineView的解決方案

每行着色NSTableView文本

更改NSTableViewCell的顏色

NSTableCellView的自定義背景顏色

我正在嘗試將每一行設置為我想要的任何顏色。 我已經讀過一些我需要NSTableRowView地方,我現在已經完成了。

根據AppleDocs ,我看到以下方法:

– drawBackgroundInRect:
– drawDraggingDestinationFeedbackInRect:
– drawSelectionInRect:
– drawSeparatorInRect:

我該如何設置各行的背景顏色? 我上面錯了嗎?

編輯:下方(也編輯標題)

由於我使用的是NSOutlineView而不是NSTableView,當我更改單元格的背景顏色時,圖像如下所示。 左邊的公開箭頭沒有着色。 有沒有辦法改變NSOutlineView的整行顏色?

這個

您可以繼承NSTableViewCell,並為其添加一個設置其顏色的方法。

NSTableViewCell已經是NSView的子類,因此在您的子類中,您將添加以下方法:

- (void)setBackgroundColor {
    self.layer.backgroundColor = CGColorCreateGenericRGB(0, 0, 0, 1.0f); // or whatever color
}

或類似的東西。 您可能希望將顏色作為方法的參數。 然后,在表視圖委托中,您可以根據傳遞給委托方法的行索引設置顏色。 例如:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

    if (indexPath.row % 2) {
        [cell setBackgroundColor:[UIColor redColor]]; // or something like that
    }
}

想出了解決方案。 實現了以下內容。

-(void)outlineView:(NSOutlineView *)outlineView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
    [self updateRowViewBackColorforItem:[outlineView itemAtRow:row]];
}

-(void)updateRowViewBackColorforStep:(myCustomItem *)customItem {
    static NSColor *color1;
    static NSColor *color2;
    static NSColor *color3;

    if (color1 == nil) {
        sharedcolorHeader = [NSColor colorWithCalibratedRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0];
    }
    if (color2 == nil) {
        sharedcolorChildren = [NSColor colorWithCalibratedRed:(x/255.0f) green:(y/255.0f) blue:(z/255.0f) alpha:1.0];
    }
    if (color3 == nil) {
        normalColor = [NSColor colorWithCalibratedRed:(255/255.0f) green:(255/255.0f) blue:(255/255.0f) alpha:1.0];
    }

    NSInteger row = [stepOutlineView rowForItem:step];
    if (row < 0) return;

    NSTableRowView *view = [myOutlineView rowViewAtRow:row makeIfNecessary:NO];

   if ([customItem type] == 1) {
        [view setBackgroundColor:sharedcolorHeader];
    } else if([customItem type] == 2) {
        [view setBackgroundColor:sharedcolorChildren];
    } else {
        [view setBackgroundColor:normalColor];
    }
}

這實際上應該依賴於數據模型中的屬性或ivars。 如果使用基於視圖的大綱視圖,則可以只為行視圖和/或單元視圖提供自定義視圖。 讓自定義視圖根據所表示對象中的數據繪制您想要的任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM