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. TypeScript完全解读(26课时)_6.TypeScript完全解读-泛型

    6.TypeScript完全解读-泛型 创建实例ts文件generics.ts 在index.ts内引入 fill是填充数组,创建的数组的元素数是times,填充的值就是接收的value的值 这里传入 ...

  2. Bootstrap表格添加搜索栏

    在学习了表格的分页后,本文尝试在表格顶部加入搜索栏,用来筛选表格的数据,先看一下效果: 未进行搜索时,有394条记录: 在输入乘车码“1”和订单号“20150120”后,只有28条记录: 此处使用了两 ...

  3. 怎样使一个INPUT框里的文字在框被点击后自动全选或清除?

    $("#smsContent").focus(function(){ this.select(); }); <input name="keywords" ...

  4. Qt开篇

    使用Qt两年有余,遇到问题多是现查现用,由于之前供职于一家保密性较强的单位,遇到的很多问题没有被记录下来.从今天开始,我会记记录自己的笔记.

  5. 洛谷 - P3935 - Calculating - 整除分块

    https://www.luogu.org/fe/problem/P3935 求: \(F(n)=\sum\limits_{i=1}^{n}d(i)\) 枚举因子\(d\),每个因子\(d\)都给其倍 ...

  6. Lightoj 1008【规律】

    25 24 23 22 21 10 11 12 13 20 9 8 7 14 19 2 3 6 15 18 1 4 5 16 17 然后把这个转化成: 17 18 19 20 21 10 11 12 ...

  7. 剑指OFFER之最大子向量和(连续子数组的最大和)(九度OJ1372)

    题目描述: HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天JOBDU测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但 ...

  8. [Xcode 实际操作]八、网络与多线程-(24)社会化分享功能开发包的安装和配置:微信、QQ、微博

    目录:[Swift]Xcode实际操作 本文将演示如何在开放平台注册应用,并获得相关的密钥信息,用于实现后面文章的微博分享功能. 一.新浪微博开放平台 [登录]->[微服务]->[粉丝服务 ...

  9. 多次页面跳转后pop回主界面的问题

    最近写代码的时候出了点BUG, 查阅资料后终于解决了. 问题原因大概是: 项目中所有的viewController都是继承自一个封装好的viewController. navigationbar, n ...

  10. Apache与Tomcat的区别和联系

    经常在用apache和tomcat等这些服务器,可是总感觉还是不清楚他们之间有什么关系,在用tomcat的时候总出现apache,总感到迷惑,到底谁是主谁是次,因此特意在网上查询了一些这方面的资料,总 ...