本文介绍了NSOutlineView不执行其子类应具有的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a Sub-Class of NSOutlineView and used the below code to make the row colors alternate.

头文件。

#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

但是问题是大纲视图不会发生代码应该执行的操作,代码正确,但是我是否需要以某种方式将大纲视图连接到代码?

But the Problem is that what the code is supposed to do doesn't happen to the Outline View, the code is correct but do I need to connect the Outline View to the code is some way?

推荐答案

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

If you instantiate the outline view in IB, you need to set the class name of your outline view to "MyOutlineView" in the Identity inspector. Remember to double click on the control so that the inner rectangle is selected and the Inspector window title is "Outline View Identity"; a single click of the control will only select the scroll view (an outline view is embedded in a scroll view).

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

If you create your outline view programmatically, just be sure to instantiate a MyOutlineView instead of an NSOutlineView:

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

其中 rect 是轮廓框架视图。

这篇关于NSOutlineView不执行其子类应具有的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:02