2013-11-02 28 views
5

我試圖修改在行展開/摺疊時我的NSOutlineView中使用的自定義NSTableRowView的外觀。我的NSOutlineViewDelegate包括:如何在NSOutlineView中重新加載自定義的NSTableRowView?

- (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item 
{ 
    ... 
    return rowView; 
} 

- (void)itemDidExpand:(NSNotification *)notification 
{ 
    NSDictionary *userInfo = [notification userInfo]; 
    id item = [userInfo objectForKey:@"NSObject"]; 
    NSOutlineView *outlineView = [notification object]; 
    [outlineView reloadItem:item]; 
} 

這不幸的是不會重新加載自定義行視圖。整個NSOutlineView

重新加載數據的工作原理:

- (void)itemDidExpand:(NSNotification *)notification 
{ 
    [outlineView reloadData]; 
} 

但隨着大量的數據這可能是耗時的,並且經常導致紡紗沙灘球。

是否有可能只爲單個頂級項目重新加載自定義行視圖?

+0

您是否在嘗試修改顏色或其他? –

+0

是的,自定義表格行視圖會覆蓋drawSeparatorInRect:和drawBackgroundInRect:根據項目是展開還是摺疊來更改顏色。 –

+0

我在下面提供了一個解決方案。讓我知道它是否工作 –

回答

5

試試下面的方法:

-(void)outlineView:(NSOutlineView *)outlineView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row { 
    aColor = [NSColor colorWithCalibratedRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0]; 
    [view setBackgroundColor:aColor]; 
} 

可以推進這一進一步得到行對象,然後確定你想爲一個特定行的顏色。

- 編輯 -

如果你想改變的親本細胞以表明它已擴大您已經實現了NSOutlineview錯誤的方法。實施如下方法:

-(void)outlineViewItemWillExpand:(NSNotification *)notification { 
    id theObject = [[notification userInfo] objectForKey:@"NSObject"]; 

    NSInteger row = [theOutlineView rowForItem:theObject]; 
    NSTableRowView *theRowView = [theOutlineView rowViewAtRow:row makeIfNecessary:NO]; 

    int r = 110; 
    int g = 110; 
    int b = 110; 

    NSColor *aColor = [NSColor colorWithCalibratedRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0]; 
    [theRowView setBackgroundColor:aColor]; 
} 
+0

謝謝Jai,我試過這個,但不幸的是outlineView:didAddRowView:forRow:只在子行上調用,而不是在展開/摺疊行。 –

+0

im confused(1)是不是被擴大/摺疊子行? (2)當添加任何行時調用它。你能提供一些代碼嗎? –

+0

父行包含顯示三角形 - 當它被點擊時,它會展開並顯示子行。那時候我想改變父行的背景顏色來表明它已經被擴展了。不幸的是,目前爲止我能夠實現的唯一方法是在整個NSOutlineView上調用reloadData。我正在尋找一種方法來強制只有父行的自定義rowView重繪自己,而無需重新加載整個表。 –

1

在你的rowViewForItem方法中試試這個。 它重新加載並重新顯示項目的數據。

- (void)reloadItem:(id)item; 
+0

rowViewForItem沒有被再次調用這一行 - 這就是我想要能夠觸發。我從itemDidExpand調用了reloadItem,並且它正在重新加載子行的行視圖,但不是用於需要更改外觀的正在展開的行。 –

相關問題