16

ビューベースの NSOutlineView で開示矢印の外観をカスタマイズしようとしています。使用が推奨されていることがわかりました

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

デリゲート メソッドを使用してそれを実現します。問題は、何らかの理由でこのメソッドが呼び出されないことです。2 つのカスタム セル ビューがあります。1 つはアイテム用、もう 1 つはヘッダー アイテム用です。このメソッドは、ビューベースのアウトライン ビューでは呼び出されないのでしょうか? Lionで何かが壊れたのでしょうか?

光を当ててください。

4

4 に答える 4

32

解決策 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;
}

ソース リストの場合は を使用しますNSOutlineViewShowHideButtonKey

解決策 2:

インターフェイスビルダー

ボタンが列に追加され、識別子が に設定され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"
于 2013-12-08T14:17:47.987 に答える
11

この回答は OS X 10.7 を念頭に置いて書かれています。OS X/macOS の新しいバージョンについては、WetFish の回答を参照してください。

このメソッドは、セル ベースのアウトライン ビューにのみ関連するため、呼び出されません。

ビュー ベースのアウトライン ビューでは、展開可能な行の行ビューでは、開閉用三角ボタンは通常のボタンです。どこに追加されるかはわかりませんが、追加されます。NSViewdidAddSubview:メソッドは、ビューが別の場所に追加される状況を正確に処理します。

したがって、 subclassNSTableRowViewと overridedidAddSubview:は次のようになります。

-(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:、新しい行ビューを返すように実装する必要があります。

その名前にもかかわらず、frameOfOutlineCellAtRow:of はNSOutlineView引き続きビュー ベースのアウトライン ビューに対して呼び出されるため、三角形を配置するために、アウトライン ビューをサブクラス化し、そのメソッドもオーバーライドすることをお勧めします。

于 2012-06-21T08:23:29.203 に答える