4

私はアウトラインビューで、カスタムセルを追加しています。カスタムセルを描画するには、Cocoaドキュメントにあるサンプルコードを参照しています。

http://www.martinkahr.com/2007/05/04/nscell-image-and-text-sample/

セルの開示画像をカスタム画像に変更したいのですが、以下のことを試してみました

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item 
    {
        if([item isKindOfClass:[NSValue class]])
        {
            MyData *pDt = (MyData *)[item pointerValue];
            if(pDt->isGroupElement())
            {
                [cell setImage:pGroupImage];
            }
        }
}

しかし、それも機能していません。開示画像を変更する他の方法はありますか。また、アイテムが展開されているか折りたたまれているかをwillDisplayCellで確認できるので、それに応じて画像を設定できます。

開示イメージを変えるのはここだけですか?

4

5 に答える 5

5

この問題を自分で調査し、ここでいくつかの回答を試したところ、言及されている他のアプローチが機能することがわかりましたが、画面のアーティファクトやその他の奇妙な動作を避けるために、より多くの手動介入を実行する必要があります.

私が見つけた最も簡単な解決策は次のとおりです。これは、ほとんどの場合に機能するはずです。

このソリューションには、列の移動など、他の非常に多くのケースをシステムが自動的に処理するという追加の利点があります。

- (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell
     forTableColumn:(NSTableColumn *)tableColumn
               item:(id)item
{
    [cell setImage:[NSImage imageNamed: @"Navigation right 16x16 vWhite_tx"]];
    [cell setAlternateImage:[NSImage imageNamed: @"Navigation down 16x16 vWhite_tx"]];
}

あなたのケースでは、これをクラス検出ロジックでまとめ、ケースに合わせてセル画像を適切に設定します。

于 2013-03-06T23:05:17.937 に答える
3

開示イメージを変更する良い方法は、ビュー ベースのアウトライン ビューを使用することです。

NSOutlineViewDelegate を使用した ViewController で:

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    CustomNSTableCellView *cell    = [outlineView makeViewWithIdentifier:tableColumn.identifier owner:self];
    cell.item                      = item;

    return cell;
}

NSOutlineView をサブクラス化し、メソッドをオーバーライドする必要があります。

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

    if ([identifier isEqualToString:NSOutlineViewDisclosureButtonKey])
    {
        // Do your customization
        // return disclosure button view

        [view setImage:[NSImage imageNamed:@"Disclosure_Categories_Plus"]];
        [view setAlternateImage:[NSImage imageNamed:@"Disclosure_Categories_Minus"]];
        [view setBordered:NO];
        [view setTitle:@""];

        return view;
    }

    return view;
}

//Frame of the disclosure view
- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row
{
    NSRect frame = NSMakeRect(4, (row * 22), 19, 19);
    return frame;
}
于 2015-02-10T14:47:14.570 に答える
1

基本的なアイデアはわかりましたが、自分でイメージを描く必要があります。私が使用するコードは次のとおりです。

- (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    NSString *theImageName;
    NSInteger theCellValue = [cell integerValue];
    if (theCellValue==1) {
        theImageName = @"PMOutlineCellOn";
    } else if (theCellValue==0) {
        theImageName = @"PMOutlineCellOff";
    } else {
        theImageName = @"PMOutlineCellMixed";
    }

    NSImage *theImage = [NSImage imageNamed: theImageName];
    NSRect theFrame = [outlineView frameOfOutlineCellAtRow:[outlineView rowForItem: item]];
    theFrame.origin.y = theFrame.origin.y +17;
    // adjust theFrame here to position your image
    [theImage compositeToPoint: theFrame.origin operation:NSCompositeSourceOver];
    [cell setImagePosition: NSNoImage];
}

ご覧のとおり、3 つの異なる画像が必要になります。1 つはON状態用、もう 1 つは状態用、もう 1 つはOFF状態用MIXEDで、2 つの中間にある必要があります。混合状態により、開始と終了のアニメーションが確実に得られます。

于 2011-02-18T07:38:28.617 に答える
0

これは私がこれまで試して取り組んできたことです。

/* 独自の開示および展開ボタンを表示しているため */

- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row {

    return NSZeroRect;
}
- (NSRect)frameOfCellAtColumn:(NSInteger)column row:(NSInteger)row {
    NSRect superFrame = [super frameOfCellAtColumn:column row:row];

    if ((column == 0) && ([self isGroupItem:[self itemAtRow:row]])) {
        return NSMakeRect(0, superFrame.origin.y, [self bounds].size.width, superFrame.size.height);
    }
    return superFrame;
}

NSOutlineView クラスをサブクラス化し、これらのメソッドをオーバーライドしました。

[self isGroupItem] は、そのグループかどうかをチェックします。しかし、1つの問題がありました。今はマウスハンドリングのように見えます:( 、グループ行をダブルクリックしてもトグルしません

于 2011-02-18T12:54:18.830 に答える