Populating View-Based Table Views using Cocoa Bindings
Populating a view-based table view using Cocoa bindings is considered an advanced topic. While it requires significantly less code (in some cases no code at all), the bindings are hard to see if you are not familiar with the interface. It is sternly suggested that you are comfortable with the techniques for using view-based table views programmatically before you move on to Cocoa bindings.
How View-Based Table Bindings Differ from Cell-Based Tables
The Cocoa bindings technique used in view-based tables differs significantly than that used in cell-based tables.
In a cell-based table you bind the table column’s content binding to the array controller’s
arrangedObjects, and then configure the column’s cell’s bindings.
In view-based bindings you bind the table view’s content binding to the array controller’s
arrangedObjects. You never bind directly to the table view’s content binding in cell-based bindings, yet this is how view-based bindings are made.
Creating the Bindings for a View-Based Table View
This example assumes that your application’s delegate has an array property called
peopleArray, which is populated with instances of a the Person class. The Person class is a subclass of
NSObject that serves as the model object and declares to properties,
The table view is a single column table that displays the image adjacent to the name. It is, essentially, one of the simplest view-based table view’s possible.
This is not intended as a tutorial, rather an illustration of the steps required for binding the contents. It is assumed that the delegate class, and nib with a single column table view with (with the Identifier set to automatic) have have already been created.
Select the MainMenu nib in the File Manager view.
This will display the Interface Builder editor which already contains the table view and the
Drag an Array Controller from the Object library to the Objects dock.
Select the Array Controller in the Objects dock and bind it to the model object array.
This step sets the
peopleArrayof the application delegate as the content array of the array controller.
Bindings are created using the Bindings inspector. See “Configure Cocoa Bindings in Interface Builder” in Xcode User Guide in the Xcode User Guide for detailed instructions on creating Cocoa bindings in Xcode 4..
Configure the Array Controller’s
Content Arraybinding as shown in the Table 5-1.
Table 5-1 Array Controller
Content Arraybinding settings
Simple Bindings App Delegate
Your application name may differ, however ensure that the binding is to the application delegate.
Model Key Path
Select the table view and then select the Bindings inspector.
Configure the bindings for the table view’s
Contentbinding as shown in Table 5-2.
Table 5-2 Table View’s
Names Array Controller
This one binding is all that is required to relate the controller to the view. Regardless of how many columns, or how complex the views are, if the model class has the fields to populate it, this is makes the most important connection to the table view.
When the connection is made to the table view’s
objectValueproperty of the
NSTableViewCellused as the cell for the table column is configured such that for each item in the model array, the table will update the
objectValueproperty to the current row’s
Select the text field in the table column cell and create the binding.
Select the image view in the table column cell and create the binding to the
All bindings of subview’s in the cell are made to the
Configure the imageView instance’s
valuebinding as shown in Table 5-4.
Table 5-4 Table Cell View’s
Table Cell View
Model Key Path
© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-07-06)