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 theContent 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 ModeView Based
    • Columns1
    • HighlightSource 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:

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的更多相关文章

  1. 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 ...

  2. How to implement updatable view with NHibernate

    see : http://msdn.microsoft.com/en-us/library/ms187956.aspx(The constrains of creation updatable vie ...

  3. 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_ ...

  4. implement Google's Open Source Slam "Cartographer" demos in ROS/rviz

    Cartographer is a backpack equipped with Simultaneous Localization and Mapping (SLAM) technology. 1. ...

  5. 【IOS笔记】Creating Custom Content View Controllers

    Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...

  6. 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 ...

  7. 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 ...

  8. View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers

    Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...

  9. 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 ...

随机推荐

  1. 1.1-1.4 sqoop概述及安装cdh版hadoop

    一.概述 Sqoop是一个在结构化数据和Hadoop之间进行批量数据迁移的工具,结构化数据可以是Mysql.Oracle等RDBMS. Sqoop底层用MapReduce程序实现抽取.转换.加载,Ma ...

  2. UVa 10891 Game of Sum (DP)

    题意:给定一个长度为n的整数序列,两个人轮流从左端或者右端拿数,A先取,问最后A的得分-B的得分的结果. 析:dp[i][j] 表示序列 i~j 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...

  3. Django - 导出项目依赖库到 requirements.txt

    两种方法: 虚拟环境: 使用 pip freeze pip freeze > requirements.txt # 这种方式推荐配合 virtualenv ,否则会把整个环境中的包都列出来. 只 ...

  4. Eclipse SVN 图标解释

    [转]http://blog.sina.com.cn/s/blog_64941c8101018dno.html - 已忽略版本控制的文件.可以通过Window → Preferences → Team ...

  5. C#简单的国际化

    1.新建一个资源文件夹,并在资源文件夹新建中英问的资源文件,如图: 2.中英文资源文档添加资源,如图: 3.Program.cs中添加根据系统语言确定中英文,这里默认为英文: using Intern ...

  6. CodeForces 723F【DFS瞎搞】

    题意: 给你一幅图,你要用这些边构造一个树, s和t两个节点的度数不能超过ds dt 而且图是保证没有环 思路: 树的性质是:无环(已经保证),无向(保证),连通(还要判断) 首先把S,T点从图里剥离 ...

  7. CTP 下单返回错误: 没有报单权限 和字段错误需要注意的问题

    没有报单权限一般被认为期货公司没有开权限, 但是更多的问题是没有填写 BrokerId, InvestorId 下单字段错误注意一个容易忽略的地方: a. order 应该全部设为0, b. orde ...

  8. 2017-7-22 NOIP模拟赛

    Digits (digits.cpp/c/pas) Description给一个关于 的多项式,并给定一个 ,求该多项式在带入该 时的值最后 位数字.Input第一行两个整数 . :之后的 行,每行两 ...

  9. IT兄弟连 JavaWeb教程 EL表达式获取对象的属性以及数组的元素

    使用${对象名.属性名} EL表达式语言可以使用点号运算符"."来访问对象的属性,例如表达式${customer.name}表示customer对象的name属性. 使用${对象名 ...

  10. Android Activity生命周期(转)

    转自 http://blog.csdn.net/android_tutor/article/details/5772285