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 ...
随机推荐
- Fitnesse中TemplateLibrary的使用方法
1.新建一个SuitePage,命名为TemplateLibrary 2.然后如下图,添加作为template的TestPage,如下面的Get 3.在Get page中添加template内容,如下 ...
- now code寒假练习赛2——处女座的砝码(找规律题+高精度题)
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { long double n ...
- 658. Find K Closest Elements
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- CodeForces599D【数学】
题意: 给出一个x,求有多少个矩阵中满足存在x个不同的正方形. 思路: (数学渣+推理渣) #include<bits/stdc++.h> using namespace std; typ ...
- laravel V层引入css 和js方法
引入css 默认引入public目录 <link rel="stylesheet" href="{{URL::asset('css/xxx.css')}}&quo ...
- P4593 [TJOI2018]教科书般的亵渎(拉格朗日插值)
传送门 首先所有亵渎的张数\(k=m+1\),我们考虑每一次使用亵渎,都是一堆\(i^k\)之和减去那几个没有出现过的\(j^k\),对于没有出现过的我们可以直接快速幂处理并减去,所以现在的问题就是如 ...
- 【SpringCloud构建微服务系列】分布式链路跟踪Spring Cloud Sleuth
一.背景 随着业务的发展,系统规模越来越大,各微服务直接的调用关系也变得越来越复杂.通常一个由客户端发起的请求在后端系统中会经过多个不同的微服务调用协同产生最后的请求结果,几乎每一个前端请求都会形成一 ...
- JavaScript 中的面向对象编程
使用JSON 来定义一个对象: <script type="text/javascript">var xiaoming = { name : 'xiaoming', a ...
- PostgreSQL-5-条件过滤
基本语法 SELECT column1, column2, columnN FROM table_name WHERE [search_condition] 操作符 =等于:<>不等于:! ...
- IOS正则表达式 (身份证、电话、汉字等常用条件筛选)
下面的正则列表 替换对应的正则规则 那个字符串就可以了 例如: //正则规则 NSString *regex = @"^((13[0-9])|(147)|(17[0-9])|(15[^ ...