Example of how to implement a view-based source list (NSOutlineView) using Cocoa Bindings
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:
- Use the Object Library (Ctrl-Cmd-Opt-3) to add an NSTreeController to your
.xib. - 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.) - With the NSTreeController still selected, use the Bindings Inspector (Cmd-Opt-7) to bind theContent Array to the AppDelegate, with a Model Key Path of
dataModel - Next use the Object Library (Ctrl-Cmd-Opt-3) to add an NSOutlineView to your
.xib. - Arrange it to your satisfaction inside the window (typically the entire height of the window, flush against the left-hand side)
- 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.)
- Using the Attributes Inspector (Cmd-Opt-4) set the NSOutlineView:
- Content Mode:
View Based - Columns:
1 - Highlight:
Source List
- Content Mode:
- 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)
- In the Object List (mentioned in #6), expand the view hierarchy of the NSOutlineView and select
Static Text - Table View Cell. - Using the Bindings Inspector (Cmd-Opt-7) bind Value to
Table Cell View, Model Key Path:objectValue.itemName(I've useditemNamein 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:

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!
Example of how to implement a view-based source list (NSOutlineView) using Cocoa Bindings的更多相关文章
- view class source code with JAD plugin in Eclipse
The default class viewer doesn't decompile the class file so you cannot open and check the source co ...
- How to implement updatable view with NHibernate
see : http://msdn.microsoft.com/en-us/library/ms187956.aspx(The constrains of creation updatable vie ...
- Django @csrf_exempt不适用于基于通用视图的类(Django @csrf_exempt does not work on generic view based class)
class ChromeLoginView(View): def get(self, request): return JsonResponse({'status': request.user.is_ ...
- implement Google's Open Source Slam "Cartographer" demos in ROS/rviz
Cartographer is a backpack equipped with Simultaneous Localization and Mapping (SLAM) technology. 1. ...
- 【IOS笔记】Creating Custom Content View Controllers
Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...
- What is the difference between database table and database view?
The database table has a physical existence in the database. A view is a virtual table, that is one ...
- How To Make A Swipeable Table View Cell With Actions – Without Going Nuts With Scroll Views
How To Make A Swipeable Table View Cell With Actions – Without Going Nuts With Scroll Views Ellen S ...
- View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers
Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...
- Table View Programming Guide for iOS---(五)---Creating and Configuring a Table View
Creating and Configuring a Table View Your app must present a table view to users before it can mana ...
随机推荐
- bugfree-解决方案的意思
BugFree的7种解决方案各自的含义: By Design - 就是这么设计的,无效的Bug Duplicate - 这个问题别人已经发现了,重复的Bug External - 是个外部因素(比如浏 ...
- Lua教程 loadfile与loadstring
在程序运行中有时需要运行用户输入的代码1.loadfile把文件编译为可执行的函数f=loadfile('\\temp\\a.lua') ----给f方法赋值print(type(f)) - ...
- ASP.NET中MessageBox的实现
asp.net中没有MessageBox这个控件,固然可以插入Winform里的MessageBox,但一般不提倡,所以只能变通实现,主要有这几种方法: 1.直接利用javascript的alert和 ...
- 3DMAX 3场景制作
1 模型制作 2 UVW贴图展开 1 打开材质编辑器,给模型一个材质(以后应该以这个材质生成uvw展开,进而生成贴图,贴图绘制完毕后,再贴回这个材质中,就完成了给模型加贴图的操作) 2 在编辑面板中添 ...
- 【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing 田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧 Descriptions: 田忌和齐王赛马,他们各有n匹马 ...
- DB2 - 编目的解释
编目(Catalog),是在本地或远程建立客户端到服务器的数据库连接的过程.其目的在于获取编目信息,即生成用来访问数据库的目录.系统数据库目录包含一个列表和指针,通过目录可以使 DB2 能够找到已知的 ...
- 结束线程方法2 Java提供的中断机制
package com.mozq.thread.interrupt; /** * 注意:调用interrupt()方法,并不会结束线程. * 结束线程的语义:需要我们自己使用3个中断方法构建. * * ...
- SQL COUNT DISTINCT 函数
定义和用法 可以一同使用 DISTINCT 和 COUNT 关键词,来计算非重复结果的数目. 语法 SELECT COUNT(DISTINCT column(s)) FROM table 例子 注意: ...
- socketserver 入门练习
个人理解: 个人感觉socketserver其实就是为服务端专门提供的一个用于解决多用户并发访问需求的一个模块 小试牛刀: 服务端socketserver_server.py import socke ...
- [题解](树的计数)luogu_P4430猴子打架_/_luogu_P4981父子
来源:题解 比较不错的博客:http://www.cnblogs.com/dirge/p/5503289.html 最后生成一颗无根树,有n^(n-2)种情况,打架的顺序有(n-1)!种 #inclu ...