有没有人找到一个清晰、简洁的示例或指南,告诉你如何使用Lion中引入的基于视图的NSOutlineView来实现源列表?我看过苹果的示例项目,但没有任何方向感或解释,我发现很难准确地掌握它们是如何工作的概念。
我知道如何使用优秀的PXSourceList作为后备,但如果可能的话,我真的很想开始使用基于视图的源代码列表。
发布于 2012-01-06 04:28:54
您使用可可绑定标记对其进行了标记,所以我认为您的意思是“使用绑定”。我举了一个简单的例子。在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编辑器中,执行以下步骤:
使用对象库(Ctrl-Cmd-Opt-3)将子项添加到您的.xib.
children (对于本例;对于您的数据,这应该是返回仍然选定的NSTreeController的子数组的任何内容,使用绑定检查器(Cmd-Opt-7)将内容数组绑定到AppDelegate,使用模型密钥路径dataModel.xib.View Based1Source List
使用绑定检查器(Cmd-Opt-7)将"Content“绑定到”树控制器“,控制器键: arrangedObjects (这是基于视图的NSTableView/NSOutlineViews的行为开始与基于NSCell的行为不同的地方)在对象列表(在#6中提到)中,展开
Static Text - Table View Cell. Bindings Inspector (Cmd-Opt-7) bind NSOutlineView Value to Table Cell View,Model Key Path:objectValue.itemName (我在假数据中使用了itemName,您可能希望使用与您的数据项的名称相对应的任何密钥)
保存。跑。您应该会看到一个源列表,并且在展开包含子节点的节点后,您可能会看到如下所示:

如果你是苹果开发者计划的成员,你应该能够访问WWDC 2011 Videos。有一个专门致力于使用基于视图的NSTableView (和NSOutlineView),它包含了非常全面的绑定内容。
希望这能有所帮助!
发布于 2013-04-01 05:33:27
看一下这个例子。
SideBarDemo
https://stackoverflow.com/questions/8090224
复制相似问题