5

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

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

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

4

2 回答 2

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];
}
于 2013-11-06T08:29:10.623 回答
1

在你的 rowViewForItem 方法中试试这个。它重新加载并重新显示项目的数据。

    - (void)reloadItem:(id)item;
于 2013-11-02T07:33:30.647 回答