![](/img/trans.png)
[英]NSTableRowView/NSTableCellView how to set custom color to selected row?
[英]How to reload a custom NSTableRowView in NSOutlineView?
我正在嘗試修改行擴展/折疊時在我的NSOutlineView中使用的自定義NSTableRowView的外觀。 我的NSOutlineViewDelegate包括:
- (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];
}
但是對於大量數據而言,這可能是耗時的,並且經常導致旋轉的沙灘球。
是否可以僅為單個頂級項重新加載自定義行視圖?
請嘗試以下方法:
-(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];
}
在rowViewForItem方法中嘗試這個。 它重新加載並重新顯示項目的數據。
- (void)reloadItem:(id)item;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.