如何在基于视图的NSOutlineView中自定义公开单元格

Nav*_*mon 14 macos cocoa disclosure nsoutlineview osx-lion

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

- (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
Run Code Online (Sandbox Code Playgroud)

委托方法来实现它.问题是由于某种原因没有调用此方法.我有2个自定义单元格视图 - 一个用于项目,第二个用于标题项目.可能是这个方法没有调用基于视图的大纲视图?狮子可能会被打破吗?

请说清楚.

Wet*_*ish 31

解决方案1:

子类NSOutlineView和覆盖 makeViewWithIdentifier:owner:

- (id)makeViewWithIdentifier:(NSString *)identifier owner:(id)owner {
    id view = [super makeViewWithIdentifier:identifier owner:owner];

    if ([identifier isEqualToString:NSOutlineViewDisclosureButtonKey]) {
        // Do your customization
    }

    return view;
}
Run Code Online (Sandbox Code Playgroud)

对于源列表使用NSOutlineViewShowHideButtonKey.

解决方案2:

Interface Builder

该按钮被添加到列中,标识符设置为NSOutlineViewDisclosureButtonKey.

在此输入图像描述

来自的官方文件 NSOutlineView.h

/* The following NSOutlineView*Keys are used by the View Based NSOutlineView to create the "disclosure button" used to collapse and expand items. The NSOutlineView creates these buttons by calling [self makeViewWithIdentifier:owner:] passing in the key as the identifier and the delegate as the owner. Custom NSButtons (or subclasses thereof) can be provided for NSOutlineView to use in the following two ways:
 1. makeViewWithIdentifier:owner: can be overridden, and if the identifier is (for instance) NSOutlineViewDisclosureButtonKey, a custom NSButton can be configured and returned. Be sure to set the button.identifier to be NSOutlineViewDisclosureButtonKey.
 2. At design time, a button can be added to the outlineview which has this identifier, and it will be unarchived and used as needed.

 When a custom button is used, it is important to properly set up the target/action to do something (probably expand or collapse the rowForView: that the sender is located in). Or, one can call super to get the default button, and copy its target/action to get the normal default behavior.

 NOTE: These keys are backwards compatible to 10.7, however, the symbol is not exported prior to 10.9 and the regular string value must be used (i.e.: @"NSOutlineViewDisclosureButtonKey").
 */
APPKIT_EXTERN NSString *const NSOutlineViewDisclosureButtonKey NS_AVAILABLE_MAC(10_9); // The normal triangle disclosure button
APPKIT_EXTERN NSString *const NSOutlineViewShowHideButtonKey NS_AVAILABLE_MAC(10_9); // The show/hide button used in "Source Lists"
Run Code Online (Sandbox Code Playgroud)


Mon*_*olo 10

这个答案是用OS X 10.7编写的,对于OS X/macOS的新版本,参考WetFish的答案

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

在基于视图的轮廓视图中,公开三角形是可扩展行的行视图中的常规按钮.我不知道它被添加,但它确实和NSViewdidAddSubview:方法处理正是添加其他地方一个鉴于这种情况.

因此,子类NSTableRowView和覆盖didAddSubview:,如下所示:

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

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

尽管名称,frameOfOutlineCellAtRow:NSOutlineView仍然被要求查看基于外形的看法,所以你的三角形的定位,你可能要继承大纲视图,并覆盖该方法,太.


Luk*_*err 8

For Swift 4.2 macOS 10.14, @WetFish's answer can be implemented as follows:

class SidebarView: NSOutlineView {

  override func makeView(withIdentifier identifier: NSUserInterfaceItemIdentifier, owner: Any?) -> NSView? {
    let view = super.makeView(withIdentifier: identifier, owner: owner)

    if identifier == NSOutlineView.disclosureButtonIdentifier {
      if let btnView = view as? NSButton {
        btnView.image = NSImage(named: "RightArrow")
        btnView.alternateImage = NSImage(named: "DownArrow")

        // can set properties of the image like the size
        btnView.image?.size = NSSize(width: 15.0, height: 15.0)
        btnView.alternateImage?.size = NSSize(width: 15.0, height: 15.0)
      }
    }
    return view
  }

}
Run Code Online (Sandbox Code Playgroud)

Looks quite nice!