2009-06-14 63 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」。請記住雙擊控件,以便選擇內部矩形,檢查器窗口標題爲「輪廓視圖標識」;只需單擊一下控件即可選擇滾動視圖(大綱視圖嵌入在滾動視圖中)。

如果編程創建您的大綱視圖,只是一定要實例化的MyOutlineView而不是NSOutlineView

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

其中rect是你的大綱視圖的框架。

+0

完美!你太棒了!只是想知道如果你能幫助我最後一件事,我將如何使網格線顏色黃色而不是藍色。我知道我將不得不改變它所說的定義的數字,但我需要改變它到什麼地方?非常感謝! – Joshua 2009-06-14 19:06:49

相關問題