如何使用Cocoa绑定实现基于视图的源列表(NSOutlineView)的示例?

Joh*_*lls 17 macos cocoa nsoutlineview cocoa-bindings

有没有人找到一个清晰,简洁的示例或指南,介绍如何使用Lion中引入的基于视图的NSOutlineView实现源列表?我看过Apple的示例项目,但没有任何方向或解释感,我发现很难掌握它们究竟是如何工作的概念.

我知道如何使用优秀的PXSourceList作为后备,但是如果可能的话,我真的想开始使用基于视图的源列表.

ipm*_*mcc 30

你用cocoa-bindings标签标记了这个,所以我假设你的意思是"带有绑定".我掀起了一个简单的例子.从Xcode中新的基于非文档的Cocoa应用程序模板开始.无论你喜欢什么,都可以打 首先,我添加了一些代码来制作一些伪数据来绑定.这是我的AppDelegate标题的样子:

#import <Cocoa/Cocoa.h>

@interface SOAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (retain) id dataModel;

@end
Run Code Online (Sandbox Code Playgroud)

这就是我的AppDelegate实现的样子:

#import "SOAppDelegate.h"

@implementation SOAppDelegate

@synthesize window = _window;
@synthesize dataModel = _dataModel;

- (void)dealloc
{
    [_dataModel release];
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application

    // Make some fake data for our source list.
    NSMutableDictionary* item1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 1", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2_1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.1", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2_2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.2", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2_2_1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.2.1", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2_2_2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.2.2", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item3 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 3", @"itemName", [NSMutableArray array], @"children", nil];

    [[item2_2 objectForKey: @"children"] addObject: item2_2_1];
    [[item2_2 objectForKey: @"children"] addObject: item2_2_2];

    [[item2 objectForKey: @"children"] addObject: item2_1];
    [[item2 objectForKey: @"children"] addObject: item2_2];

    NSMutableArray* dataModel = [NSMutableArray array];

    [dataModel addObject: item1];
    [dataModel addObject: item2];
    [dataModel addObject: item3];

    self.dataModel = dataModel;
}

@end
Run Code Online (Sandbox Code Playgroud)

对我创建的虚假数据结构没有特别的意义,我只想展示一些具有几个子级别的东西,等等.唯一重要的是你在Interface Builder中的绑定中指定的关键路径与数据中的键(在这种情况下为假数据).

然后选择MainMenu.xib文件.在IB编辑器中,执行以下步骤:

  1. 使用对象库(Ctrl-Cmd-Opt-3)将NSTreeController添加到您的.xib.
  2. 选择NSTreeController,并使用Attributes Inspector(Cmd-Opt-4)将Key Paths> Children设置为children(对于此示例;对于您的数据,这应该是返回子对象数组的任何内容.)
  3. 在仍然选择NSTreeController的情况下,使用Bindings Inspector(Cmd-Opt-7)将Content Array绑定到AppDelegate,其模型键路径为dataModel
  4. 接下来使用对象库(Ctrl-Cmd-Opt-3)将NSOutlineView添加到您的.xib.
  5. 在窗户内安排令您满意(通常是窗户的整个高度,与左侧齐平)
  6. 选择NSOutlineView(注意,第一次你点击它,你可能已经选择了包含它的NSScrollView,点击它第二次,你就会有钻,下至NSOutlineView本身.注意,这是MUCH如果容易你扩展了IB编辑器左边所有对象所在的区域 - 这允许你将对象看作一棵树,然后导航并选择它们.)
  7. 使用Attributes Inspector(Cmd-Opt-4)设置NSOutlineView:
    • 内容模式:View Based
    • 专栏:1
    • 亮点:Source List
  8. 使用Bindings Inspector(Cmd-Opt-7)将"Content"绑定到"Tree Controller",Controller Key:arrangeObjects(这是基于视图的NSTableView/NSOutlineViews的行为开始偏离基于NSCell的行为的地方)
  9. 在对象列表(在#6中提到)中,展开NSOutlineView的视图层次结构并选择Static Text - Table View Cell.
  10. 使用Bindings Inspector(Cmd-Opt-7)将Value绑定到Table Cell ViewModel Key Path :( objectValue.itemNameitemName在伪数据中使用过,你会想要使用与数据项名称相对应的任何键)

保存.跑.您应该看到一个源列表,一旦您使用子节点扩展节点,您可能会看到如下内容:

在此输入图像描述

如果您在Apple开发人员计划中,则应该能够访问WWDC 2011视频.有一个专门用于处理基于视图的NSTableView(和NSOutlineView),它包括非常全面的绑定覆盖.

希望有所帮助!


Cor*_*ory 5

看看这个例子.

SideBarDemo