2011-02-18 54 views
2

在我NSOutlineview我使用的是从NSTextFieldCell子类自定义单元格, 我需要绘制不同的颜色组行和正常行,它的选择时,NSOutlineView更改组一行彩色

要做到这一点,我这样做之后,

-(id)_highlightColorForCell:(NSCell *)cell 
{ 
    return [NSColor colorWithCalibratedWhite:0.5f alpha:0.7f]; 
} 

是的,我知道它的私有API,但我无法找到任何其他方式, 这是工作非常好正常行,但对集团行没有影响,有什么办法改变组颜色,

亲切的问候 Rohan

回答

3

你实际上可以做到这一点,而不依赖于私人API的,至少如果你愿意要求Mac OS X 10.4或更高版本。

放入你的子类中的以下内容:

- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 
{ 
    // Returning nil circumvents the standard row highlighting. 
    return nil; 
} 

然后子类NSOutlineView和重新实现的方法, - (空)highlightSelectionInClipRect:(的NSRect)clipRect;

下面是吸引非组行一种颜色,另一个组行

- (void)highlightSelectionInClipRect:(NSRect)clipRect 
{ 
    NSIndexSet *selectedRowIndexes = [self selectedRowIndexes]; 
    NSRange visibleRows = [self rowsInRect:clipRect]; 

    NSUInteger selectedRow = [selectedRowIndexes firstIndex]; 
    while (selectedRow != NSNotFound) 
    { 
    if (selectedRow == -1 || !NSLocationInRange(selectedRow, visibleRows)) 
    { 
     selectedRow = [selectedRowIndexes indexGreaterThanIndex:selectedRow]; 
     continue; 
    } 

    // determine if this is a group row or not 
    id delegate = [self delegate]; 
    BOOL isGroupRow = NO; 
    if ([delegate respondsToSelector:@selector(outlineView:isGroupItem:)]) 
    { 
     id item = [self itemAtRow:selectedRow]; 
     isGroupRow = [delegate outlineView:self isGroupItem:item]; 
    } 

    if (isGroupRow) 
    { 
     [[NSColor alternateSelectedControlColor] set]; 
    } else { 
     [[NSColor secondarySelectedControlColor] set]; 
    } 

    NSRectFill([self rectOfRow:selectedRow]); 
    selectedRow = [selectedRowIndexes indexGreaterThanIndex:selectedRow]; 
    } 
} 
+0

@thanks cayden,我不想故意使用私有API,将尝试了这一点的例子,但希望知道一件事,我该如何更改组的行颜色 – Amitg2k12 2011-02-19 05:18:59