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

发布于 2024-12-15 02:02:23 字数 173 浏览 2 评论 0原文

有没有人找到关于如何使用 Lion 中引入的基于视图的 NSOutlineView 实现源列表的清晰、简洁的示例或指南?我看过苹果的示例项目,但没有任何方向感或解释,我发现很难掌握它们到底如何工作的概念。

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

Has anybody found a clear, concise example or guide on how to implement a source list using the view-based NSOutlineView introduced in Lion? I've looked at Apple's example project, but without any sense of direction or explanation, I'm finding it difficult to grasp the concept of exactly how they work.

I know how to use the excellent PXSourceList as a fallback, but would really like to start using view-based source lists instead if at all possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-12-22 02:02:23

您用 cocoa-bindings 标签标记了它,所以我假设您的意思是“带有绑定”。我举了一个简单的例子。从 Xcode 中新的非基于文档的 Cocoa 应用程序模板开始。随便你怎么称呼它。首先,我添加了一些代码来绑定一些假数据。这是我的 AppDelegate 标头的样子:

#import <Cocoa/Cocoa.h>

@interface SOAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (retain) id dataModel;

@end

这是我的 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

我创建的假数据结构没有特别的意义,我只是想展示一些带有几个子级别的东西,等等。唯一重要的是是您在 Interface Builder 的绑定中指定的键路径与数据中的键(在本例中为假数据)对齐。

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

  1. 使用对象库 (Ctrl-Cmd-Opt-3) 将 NSTreeController 添加到 .xib 中。
  2. 选择 NSTreeController,并使用属性检查器 (Cmd-Opt-4) 设置Key Paths > Childrenchildren (对于本例;对于您的数据,这应该是返回子对象数组的任何内容。)
  3. 在仍选择 NSTreeController 的情况下,使用绑定检查器 (Cmd-Opt- 7) 将 Content Array 绑定到 AppDelegate,模型键路径为 dataModel
  4. 接下来使用对象库 (Ctrl-Cmd-Opt-3) 添加 NSOutlineView到您的.xib
  5. 在窗口内将其排列到您满意的位置(通常是窗口的整个高度,与左侧齐平)
  6. 选择 NSOutlineView(请注意,第一次单击它时,您可能已经选择了包含它的 NSScrollView。单击第二次,您将深入到 NSOutlineView 本身。请注意,如果您扩大 IB 编辑器左侧所有对象所在的区域,这会容易得多。这可以让你看到 使用属性
  7. 检查器 (Cmd-Opt-4) 设置 NSOutlineView:
    • 内容模式基于视图
    • 1
    • 突出显示来源列表
  8. 使用绑定检查器 (Cmd-Opt-7) 将“Content”绑定到“Tree Controller”,控制器键:arrangedObjects(这是基于视图的 NSTableView/NSOutlineViews 的行为开始偏离的地方基于NSCell )
  9. 在对象列表(#6 中提到)中,展开 NSOutlineView 的视图层次结构并选择静态文本 - 表视图单元格
  10. 使用绑定检查器 (Cmd-Opt-7) 将 Value 绑定到 Table Cell View,模型键路径:objectValue.itemName (我已经在假数据中使用了 itemName,您需要使用与数据项名称相对应的键)

保存。跑步。您应该看到一个源列表,一旦您展开了带有子节点的节点,您可能会看到如下内容:

加入了 Apple 开发者计划,您应该能够访问 WWDC 2011视频。有一个专门致力于使用基于视图的 NSTableView(和 NSOutlineView),并且它包含相当全面的绑定覆盖。

希望有帮助!

You tagged this with the cocoa-bindings tag, so I assume you mean "with bindings." I whipped up a quick example. Start from a new non-document-based Cocoa Application template in Xcode. Call it whatever you like. First I added some code to make some fake data to bind to. Here's what my AppDelegate header looks like:

#import <Cocoa/Cocoa.h>

@interface SOAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (retain) id dataModel;

@end

And here's what my AppDelegate implementation looks like:

#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

There's no particular significance to the fake data structure I created, I just wanted to show something with a couple of sub-levels, etc. The only thing that matters is that the key paths you specify in the bindings in Interface Builder line up with the keys in your data (fake data in this case.)

Then select the MainMenu.xib file. In the IB editor, do the following steps:

  1. Use the Object Library (Ctrl-Cmd-Opt-3) to add an NSTreeController to your .xib.
  2. Select the NSTreeController, and using the Attributes Inspector (Cmd-Opt-4) set Key Paths > Children to children (for this example; For your data, this should be whatever returns the array of child objects.)
  3. With the NSTreeController still selected, use the Bindings Inspector (Cmd-Opt-7) to bind the Content Array to the AppDelegate, with a Model Key Path of dataModel
  4. Next use the Object Library (Ctrl-Cmd-Opt-3) to add an NSOutlineView to your .xib.
  5. Arrange it to your satisfaction inside the window (typically the entire height of the window, flush against the left-hand side)
  6. Select the NSOutlineView (note that the first time you click on it, you have likely selected the NSScrollView that contains it. Click on it a second time and you'll have drilled-down to the NSOutlineView itself. Note that this is MUCH easier if you widen the area on the left of the IB editor where all the objects are -- this allows you see the objects as a tree, and navigate and select them that way.)
  7. Using the Attributes Inspector (Cmd-Opt-4) set the NSOutlineView:
    • Content Mode: View Based
    • Columns: 1
    • Highlight: Source List
  8. Using the Bindings Inspector (Cmd-Opt-7) bind "Content" to "Tree Controller", Controller Key: arrangedObjects (This is where the behavior of View-based NSTableView/NSOutlineViews starts to diverge from NSCell-based ones)
  9. In the Object List (mentioned in #6), expand the view hierarchy of the NSOutlineView and select Static Text - Table View Cell.
  10. Using the Bindings Inspector (Cmd-Opt-7) bind Value to Table Cell View, Model Key Path: objectValue.itemName (I've used itemName in the fake data, you would want to use whichever key corresponded to the name of your data items)

Save. Run. You should see a source list, and once you've expanded the nodes with children, you might see something like this:

enter image description here

If you're in the Apple Developer Program, you should be able to access the WWDC 2011 Videos. There's one specifically dedicated to working with View-based NSTableView (and NSOutlineView) and it includes pretty thorough coverage of bindings.

Hope that helps!

隱形的亼 2024-12-22 02:02:23

看一下这个例子。

SideBarDemo

Take a look at this example.

SideBarDemo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文