如何在NSOutlineView中重新加载自定义NSTableRowView?

Tob*_*upt 5 cocoa objective-c

我正在尝试修改行扩展/折叠时在我的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];
}
Run Code Online (Sandbox Code Playgroud)

遗憾的是,这不会重新加载自定义行视图.

重新加载整个NSOutlineView的数据:

- (void)itemDidExpand:(NSNotification *)notification
{
    [outlineView reloadData];
}
Run Code Online (Sandbox Code Playgroud)

但是对于大量数据而言,这可能是耗时的,并且经常导致旋转的沙滩球.

是否可以仅为单个顶级项重新加载自定义行视图?

iOS*_*com 6

请尝试以下方法:

-(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];
}
Run Code Online (Sandbox Code Playgroud)

您可以进一步前进以获取行对象,然后确定特定行所需的颜色.

- 编辑 -

如果您希望更改父单元格视图以显示它已被展开,那么您已经为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];
}
Run Code Online (Sandbox Code Playgroud)