Objective c 如何更改NSOutlineView每个单元格的颜色或NSTableCellView

Objective c 如何更改NSOutlineView每个单元格的颜色或NSTableCellView,objective-c,cocoa,Objective C,Cocoa,我已经研究了许多问题,但对于基于视图的NSOutlineView,找不到一个好的解决方案 我试着将每一行设置为我想要的任何颜色。我在某个地方读到,我需要将NSTableRowView子类化,我现在已经完成了 根据这篇文章,我看到了以下方法: – drawBackgroundInRect: – drawDraggingDestinationFeedbackInRect: – drawSelectionInRect: – drawSeparatorInRect: 如何设置各行的背景色?我走错

我已经研究了许多问题,但对于基于视图的NSOutlineView,找不到一个好的解决方案

我试着将每一行设置为我想要的任何颜色。我在某个地方读到,我需要将
NSTableRowView
子类化,我现在已经完成了

根据这篇文章,我看到了以下方法:

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

这实际上应该依赖于数据模型中的属性或IVAR。 如果使用基于视图的大纲视图,则可以为行视图和/或单元视图创建自定义视图。
让自定义视图根据所表示对象中的数据绘制所需内容

想出了一个解决办法。实施了以下内容

-(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];
    }
}
-(void)outlineView:(NSOutlineView*)outlineView didAddRowView:(NSTableRowView*)rowView for row:(NSInteger)row{
[self-updateRowViewBackColorforItem:[outlineView itemAtRow:行]];
}
-(void)updateRowViewBackColorforStep:(myCustomItem*)customItem{
静态NSColor*color1;
静态NSColor*color2;
静态NSColor*color3;
if(color1==nil){
sharedcolorHeader=[NSColor带校准颜色:(r/255.0f)绿色:(g/255.0f)蓝色:(b/255.0f)alpha:1.0];
}
if(color2==nil){
sharedcolorChildren=[N校准后的颜色:(x/255.0f)绿色:(y/255.0f)蓝色:(z/255.0f)alpha:1.0];
}
if(color3==nil){
normalColor=[NSColor color with calibered:(255/255.0f)绿色:(255/255.0f)蓝色:(255/255.0f)alpha:1.0];
}
NSInteger行=[stepOutlineView行forItem:step];
如果(行<0)返回;
NSTableRowView*视图=[myOutlineView rowViewAtRow:行生成如果需要:否];
如果([customItem type]==1){
[查看setBackgroundColor:sharedcolorHeader];
}else if([customItem type]==2){
[查看setBackgroundColor:sharedcolorChildren];
}否则{
[查看setBackgroundColor:normalColor];
}
}

将此方法添加到init方法是什么意思?对不起,不是init方法,只是子类。另外,添加了使用此方法的表视图示例。编辑了上面的问题我已经有自定义视图了。这就是我被困的地方。如何使用自定义视图进行绘制?如果我有只绘制一种颜色的示例代码,我将知道如何绘制其余的颜色。因此,如果您representedObject有一些方法来识别正确的样式,最好是在编码简单之前的属性,您的视图单元格可以询问representedObject有关属性XYZ的信息。如果是X,则执行XStyling。如果IB不允许,您可以在代码中进行绑定。样式部分与任何条件逻辑相同,但可能侧重于确定在drawRect中要做什么。只需在视图类中添加所需的属性。说一个NSInteger标志设置和使用,以确定如何绘制。我认为你没有理解我的要求。。我知道这一切。我有条件逻辑(XYZ)来做这个逻辑。。我把一切都安排好了。但我的问题是-->如何绘制?绘图代码是什么?这是我一直在问的问题。。我不知道抽签密码……啊哈。那就这样了。你到底想画什么?在drawRect中,包含视图的背景非常简单,但不容易猜测。我可以给你一些提示。我只想画一个蓝色的背景。一旦我看到蓝色的代码,我就可以换成其他颜色。我只想让手机有一个蓝色的背景。但不像上面显示的那样,我希望整个细胞都是蓝色的,而不是一部分。我应该在drawRect中输入什么代码?您需要为行视图设置绘图样式。这是什么:(x/255.0f)?定义浮动的快捷方式?我不知道为什么会这样,但事实就是这样。通过快速的谷歌搜索,我可以找到原因,但我没有时间。颜色值x必须除以255,才能显示您期望的颜色。啊,我明白了:颜色值在0..1之间浮动,所以1表示“全色”,0表示无颜色。几年前,我在简单的8位VGA屏幕上编码,所以255是8位的,所以你必须将8位值映射到0到1的范围。这几天看到这一点我很困惑。