NSOutlineView没有做它应该做的事情在它的子类中

时间:2009-06-14 18:47:42

标签: objective-c cocoa

我创建了一个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

但问题是代码应该做的事情不会发生在Outline视图中,代码是正确的但是我需要将Outline视图连接到代码吗?

1 个答案:

答案 0 :(得分:4)

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

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

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

其中rect是大纲视图的框架。