12

学習目的で、セルベースのNSOutlineViewをビューベースのNSOutlineViewに変換したいと思います。

基本的に私は次のことを望みます:

  • 通常のセルの代わりに、「画像とテキストの表のセルビュー」が欲しい
  • 画像はストックNSApplicationIconにすることができ、テキストは「helloworld」にすることができます:)
  • バインディングを使用せずにこれを実行したいNSTreeController

これが「世界で最も単純なNSOutlineView」の例ですhttp://www.cocoasteam.com/Cocoa_Steam/Worlds_Simplest_Demo.html

誰かがそれを変更してビューベースにし、上記のように機能させることができるかどうか疑問に思います:) :)

私はアップルの例を見て、インターネット上の他の場所を検索しようとしましたが、それでも機能させることができません-事前に感謝します:)

4

4 に答える 4

9

私はまさにそれを行う小さなサンプルプロジェクトを作成しました。

  • アイテムのリストを表示する
  • マスターディテール方式でアイテムを編集する
  • アイテムの削除と追加
  • バインディングの使用

githubでbesi/mac-quickiesをチェックしてください。ほとんどの作業はIBで行われるか、AppDelegateで見つけることができます

スクリーンショット

于 2013-10-11T13:03:48.677 に答える
8

さて、あなたはセルNSOutlineView付きが欲しいですよね?ImageAndTextCell

この種の最も典型的な例の1つである単純なファイルエクスプローラーを実行してみましょう。

必要なもの:

  • (として、NSOutlineViewAppDelegateにアウトラインを配置しますfileOutlineView
  • 次の識別子を使用してアウトラインに3つの列を作成します(Interface Builderで設定します):NameColumn、、SizeColumnModifiedColumn

さて、残りはすべてプログラムで行いますので、何が起こっているのかがよくわかります...

設定方法(例:)- (void)awakeFromNib

// set the Data Source and Delegate
[fileOutlineView setDataSource:(id<NSOutlineViewDataSource>)self];
[fileOutlineView setDelegate:(id<NSOutlineViewDelegate>)self];

// set the first column's cells as `ImageAndTextCell`s
ImageAndTextCell* iatc = [[ImageAndTextCell alloc] init];
[iatc setEditable:NO];
[[[fileOutlineView tableColumns] objectAtIndex:0] setDataCell:iatc];

ドットをつなぐ:

/*******************************************************
 *
 * OUTLINE-VIEW DATASOURCE
 *
 *******************************************************/

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{        
    if ([item isFolder])
        return YES;
    else
        return NO;
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{    
    if (item==nil)
    {
        // Root
        return [[filePath folderContentsWithPathAndBackIgnoringHidden] count];
    }
    else
    {        
        if ([item isFolder])
        {
            return [[item folderContentsWithPathAndBackIgnoringHidden] count];
        }
        else
        {
            return 0;
        }
    }
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    if (item == nil)
    { 
        // Root
        return [[filePath folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
    }

    if ([item isFolder])
    {
        return [[item folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
    }

    // File
    return nil;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item
{          
    if ([[theColumn identifier] isEqualToString:@"NameColumn"])
    {
        return [item lastPathComponent];
    }
    else if ([[theColumn identifier] isEqualToString:@"SizeColumn"])
    {
        if ([item isFolder]) return @"--";
        else return [NSString stringWithFormat:@"%d",[item getFileSize]];
    }
    else if ([[theColumn identifier] isEqualToString:@"ModifiedColumn"])
    {
        if ([item isFolder]) return @"";
        else return [NSString stringWithFormat:@"%@",[item getDateModified]];
    }

    // Never reaches here
    return nil;
}

/*******************************************************
 *
 * OUTLINE-VIEW DELEGATE
 *
 *******************************************************/

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
    return YES;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
{
    return NO;
}

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    [cell setDrawsBackground:NO];

    if ([item isFileHidden]) [cell setTextColor:[NSColor grayColor]];
    else [cell setTextColor:[NSColor whiteColor]];

    if ([[tableColumn identifier] isEqualToString:@"NameColumn"])
    {
        if ([item isFolder])
            [cell setImage:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] size:15.0];
        else
            [cell setImage:[[NSWorkspace sharedWorkspace] iconForFile:item] size:15.0];

        if ([item isFileHidden])
        {
            [cell setFileHidden:YES];
        }
        else
        {
            [cell setFileHidden:NO];
        }

    }

}

ヒント:クラスはここにあります。また、私が使用している他のいくつかのメソッドにも気付くでしょう。これらは明らかにCocoaではサポートされていません(たとえばまたは)が、自分で実装するのはそれほど難しくありません...) ImageAndTextCellisFileHiddenisFolderfolderContentsWithPathAndBackIgnoringHidden

于 2012-04-23T06:39:42.470 に答える
1

ビューをOutlineView列に戻すには、objectValueを返すデータソースメソッドを使用する代わりに、次のようにします。

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item

ビューを返すデータソースメソッドを使用してください!!!!!!!!:

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item

他のすべては同じです(最小要求は最初の3つのデータソースメソッドであり、デリゲートメソッドは必要ありません)が、セルベースでのみ呼び出されるwilldisplaycellを使用することはできません。このように、viefortablecolumnメソッドのビューに対してすべてを実行します。 :

if ([[tableColumn identifier] isEqualToString:@"YourColumnIdentifier"]){
    NSTableCellView *cell = [outlineView makeViewWithIdentifier:@"YourViewsIdentifier" owner:self];
    [cell.textField setStringValue:[(YourItem *)item name]];
    [cell.imageView setImage:[(YourItem *)item image]];
    return cell;
}

return nil;

識別子を設定し、OutlineViewをビューベース(IB ...)に設定することを忘れないでください。

于 2013-11-29T07:42:58.453 に答える
0

TableViewPlaygroundを確認してください。また、WWDC2011の ビューベースのNSTableViewBasicからAdvancedまでを確認してください。

于 2012-04-10T08:23:50.043 に答える