【问题标题】:NSOutlineView not doing what it has should do in it's subclassNSOutlineView 没有做它应该在它的子类中做的事情
【发布时间】:2009-06-14 18:47:42
【问题描述】:

我创建了一个 NSOutlineView 的子类,并使用下面的代码使行颜色交替显示。

头文件。

#import <Cocoa/Cocoa.h>


@interface MyOutlineView : NSOutlineView {
}

- (void) drawStripesInRect:(NSRect)clipRect;

@end

实施文件。

#import "MyOutlineView.h"

// RGB values for stripe color (light blue)
#define STRIPE_RED   (237.0 / 255.0)
#define STRIPE_GREEN (243.0 / 255.0)
#define STRIPE_BLUE  (254.0 / 255.0)
static NSColor *sStripeColor = nil;

@implementation MyOutlineView

// This is called after the table background is filled in,
// but before the cell contents are drawn.
// We override it so we can do our own light-blue row stripes a la iTunes.
- (void) highlightSelectionInClipRect:(NSRect)rect {
    [self drawStripesInRect:rect];
    [super highlightSelectionInClipRect:rect];
}

// This routine does the actual blue stripe drawing,
// filling in every other row of the table with a blue background
// so you can follow the rows easier with your eyes.
- (void) drawStripesInRect:(NSRect)clipRect {
    NSRect stripeRect;
    float fullRowHeight = [self rowHeight] + [self intercellSpacing].height;
    float clipBottom = NSMaxY(clipRect);
    int firstStripe = clipRect.origin.y / fullRowHeight;
    if (firstStripe % 2 == 0)
        firstStripe++;   // we're only interested in drawing the stripes
    // set up first rect
    stripeRect.origin.x = clipRect.origin.x;
    stripeRect.origin.y = firstStripe * fullRowHeight;
    stripeRect.size.width = clipRect.size.width;
    stripeRect.size.height = fullRowHeight;
    // set the color
    if (sStripeColor == nil)
        sStripeColor = [[NSColor colorWithCalibratedRed:STRIPE_RED
                                                  green:STRIPE_GREEN
                                                   blue:STRIPE_BLUE
                                                  alpha:1.0] retain];
    [sStripeColor set];
    // and draw the stripes
    while (stripeRect.origin.y < clipBottom) {
        NSRectFill(stripeRect);
        stripeRect.origin.y += fullRowHeight * 2.0;
    }
}

@end

但问题是代码应该做的事情不会发生在大纲视图上,代码是正确的,但是我需要以某种方式将大纲视图连接到代码吗?

【问题讨论】:

    标签: objective-c cocoa


    【解决方案1】:

    如果在 IB 中实例化大纲视图,则需要在身份检查器中将大纲视图的类名设置为“MyOutlineView”。记得双击控件,使内部矩形被选中,Inspector 窗口标题为“Outline View Identity”;单击控件只会选择滚动视图(大纲视图嵌入在滚动视图中)。

    如果您以编程方式创建大纲视图,请务必实例化 MyOutlineView 而不是 NSOutlineView

    MyOutlineView *outlineView = [[MyOutlineView alloc] initWithFrame:rect];
    

    rect 是大纲视图的框架。

    【讨论】:

    • 完美!你太棒了!只是想知道您是否可以帮我做最后一件事,我如何使网格线颜色为黄色而不是蓝色。我知道我必须更改它说定义的数字,但是我需要将其更改为什么?非常非常感谢!
    • STRIPE_RED、STRIPE_GREEN 和 STRIPE_BLUE 是颜色的 RGB 值。如果您找到所需黄色的 RGB 值,请相应地更改这些定义,保留 /255.0,因为它们显然表示为 0 到 1 之间的小数。
    猜你喜欢
    • 2011-09-28
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多