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

时间:2013-11-02 05:36:06

标签: objective-c cocoa

我正在尝试修改行扩展/折叠时在我的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];
}

但是,由于数据量很大,这可能非常耗时,并且通常会导致旋转的沙滩球。

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

2 个答案:

答案 0 :(得分: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];
}

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

- 的修改 -

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

答案 1 :(得分:1)

在rowViewForItem方法中尝试此操作。 它重新加载并重新显示项目的数据。

    - (void)reloadItem:(id)item;