如何在基于视图的NSOutlineView中自定义公开单元 [英] How to customize disclosure cell in view-based NSOutlineView

查看:295
本文介绍了如何在基于视图的NSOutlineView中自定义公开单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在基于视图的NSOutlineView中自定义显示箭头的外观.我看到建议使用

I'm trying to customize the disclosure arrow appearance in my view-based NSOutlineView. I saw that it's recommended to use

- (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item

代表方法来实现它.问题是由于某种原因未调用此方法.我有2个自定义单元格视图-一个用于项目,第二个用于标题项目.可能不是基于视图的大纲视图不调用此方法吗?可能是Lion发生了故障?

delegate method to achieve it. The problem is that this method is not called for some reason. I have 2 custom cell views - one for item and second for header item. May be this method is not called for view-based outline views? May be something became broken in Lion?

请阐明一下.

推荐答案

此答案是考虑到OS X 10.7的,对于OS X/macOS的较新版本,请参考WetFish的答案

该方法不会被调用,因为它仅与基于单元格的轮廓视图相关.

That method does not get called because it is only relevant for cell based outline views.

在基于视图的轮廓视图中,显示三角形是可展开行的行视图中的常规按钮.我不知道在哪里添加它,但是确实如此,NSViewdidAddSubview:方法恰好处理了将视图添加到其他地方的情况.

In a view based outline view, the disclosure triangle is a regular button in the row view of expandable rows. I don't know where it gets added, but it does, and NSView's didAddSubview: method handles exactly that situation of a view being added somewhere else.

因此,是子类NSTableRowView,并覆盖了didAddSubview:,如下所示:

Hence, subclass NSTableRowView, and override didAddSubview:, like this:

-(void)didAddSubview:(NSView *)subview
{
    // As noted in the comments, don't forget to call super:
    [super didAddSubview:subview];

    if ( [subview isKindOfClass:[NSButton class]] ) {
        // This is (presumably) the button holding the 
        // outline triangle button.
        // We set our own images here.
        [(NSButton *)subview setImage:[NSImage imageNamed:@"disclosure-closed"]];
        [(NSButton *)subview setAlternateImage:[NSImage imageNamed:@"disclosure-open"]];
    }
}

当然,您的大纲视图的委托必须实现outlineView:rowViewForItem:才能返回新的行视图.

Of course, your outline view's delegate will have to implement outlineView:rowViewForItem: to return the new row view.

尽管名称很大,但仍会为基于视图的轮廓视图调用NSOutlineViewframeOfOutlineCellAtRow:,因此对于三角形的定位,您可能还想继承轮廓视图并覆盖该方法.

Despite the name, frameOfOutlineCellAtRow: of NSOutlineView still gets called for view based outline views, so for the positioning of your triangle, you might want to subclass the outline view and override that method, too.

这篇关于如何在基于视图的NSOutlineView中自定义公开单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆