2009-06-14 6 views
0

NSOutlineViewのサブクラスを作成し、以下のコードを使用して行の色を交互にしました。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 

しかし、問題は、コードがアウトラインビューには発生しません行うことになっているもの、コードが正しいですが、私はコードにアウトラインビューを接続する必要がないということですか?

答えて

4

IBでアウトラインビューをインスタンス化する場合、アイデンティティインスペクタのアウトラインビューのクラス名を "MyOutlineView"に設定する必要があります。内側の四角形が選択され、インスペクタウィンドウのタイトルが「アウトラインビューアイデンティティ」になるようにコントロールをダブルクリックすることを忘れないでください。コントロールを1回クリックするだけでスクロール・ビューが選択されます(アウトライン・ビューはスクロール・ビューに組み込まれています)。

あなたがプログラムであなたのアウトラインビューを作成する場合は、だけではなく、NSOutlineViewMyOutlineViewをインスタンス化するようにしてください:

rectはあなたのアウトラインビューのフレームである
MyOutlineView *outlineView = [[MyOutlineView alloc] initWithFrame:rect]; 

+0

パーフェクト!あなたが素晴らしいです!あなたが1つの最後のもので私を助けることができるかどうかだけ考えて、どのように私は青の代わりにグリッド線の色を黄色にしますか?私はそれが何を定義するが、私はそれを変更する必要があるでしょう番号を変更する必要があります知っている?非常にありがとう! – Joshua

+0

STRIPE_RED、STRIPE_GREENおよびSTRIPE_BLUEは、色のRGB値です。あなたが望む黄色のRGB値を見つけたら、0と1の間の小数点として明示的に表現されているので、/ 255を保持してそれに応じて定義を変更します。 –

関連する問題