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编辑器中,执行以下步骤:
.xib
.children
(对于此示例;对于您的数据,这应该是返回子对象数组的任何内容.)dataModel
.xib
.View Based
1
Source List
Static Text - Table View Cell
.Table Cell View
Model Key Path :( objectValue.itemName
我itemName
在伪数据中使用过,你会想要使用与数据项名称相对应的任何键)保存.跑.您应该看到一个源列表,一旦您使用子节点扩展节点,您可能会看到如下内容:
如果您在Apple开发人员计划中,则应该能够访问WWDC 2011视频.有一个专门用于处理基于视图的NSTableView(和NSOutlineView),它包括非常全面的绑定覆盖.
希望有所帮助!
归档时间: |
|
查看次数: |
13439 次 |
最近记录: |