有没有人找到一份清晰简洁的示例或指南,介绍如何使用 Lion 中引入的基于视图的 NSOutlineView 实现源列表?我已经查看了苹果的示例项目,但是没有方向感或解释,我很难理解它们的工作原理。
我知道如何使用优秀的 PXSourceList 作为备选方案,但如果可能的话,我真的很想开始使用基于视图的源列表。
有没有人找到一份清晰简洁的示例或指南,介绍如何使用 Lion 中引入的基于视图的 NSOutlineView 实现源列表?我已经查看了苹果的示例项目,但是没有方向感或解释,我很难理解它们的工作原理。
我知道如何使用优秀的 PXSourceList 作为备选方案,但如果可能的话,我真的很想开始使用基于视图的源列表。
你在标签中加入了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 编辑器中执行以下步骤:
.xib
。children
(对于该示例;对于你的数据,应该是返回子对象数组的任何内容)。dataModel
。.xib
。视图为基础
1
源列表
静态文本 - 表格视图单元格
。表格视图单元格视图
,模型键路径:objectValue.itemName
(我在虚假数据中使用了 itemName
,你应该使用与你的数据项名称对应的任何键)。保存。运行。 你应该会看到一个源列表,一旦你展开了具有子级的节点,就可能会看到像这样的东西:
如果你是苹果开发者计划的成员,你应该可以访问 WWDC 2011 视频。其中一个视频专门介绍了如何使用基于视图的 NSTableView (和 NSOutlineView),并包括对绑定的相当全面的覆盖。
希望能有所帮助!