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

查看:19
本文介绍了如何在基于视图的 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天全站免登陆