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, name and image.

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.

bullet
To create bindings for the person table view
  1. Select the MainMenu nib in the File Manager view.

    This will display the Interface Builder editor which already contains the table view and the NSTableCellView.

  2. Drag an Array Controller from the Object library to the Objects dock.

  3. Select the Array Controller in the Objects dock and bind it to the model object array.

    This step sets the peopleArray of 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 Array binding as shown in the Table 5-1.

    Table 5-1  Array Controller Content Array binding settings

    Binding Field

    Value

    Bind to:

    Simple Bindings App Delegate

    Your application name may differ, however ensure that the binding is to the application delegate.

    Model Key Path

    peopleArray

  4. Select the table view and then select the Bindings inspector.

  5. Configure the bindings for the table view’s Content binding as shown in Table 5-2.

    Table 5-2  Table View’s Content binding settings

    Binding Field

    Value

    Bind to:

    Names Array Controller

    Controller Key

    arrangedObjects

    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 Content binding, the objectValue property of the NSTableViewCell used as the cell for the table column is configured such that for each item in the model array, the table will update the objectValue property to the current row’s Person object.

  6. Select the text field in the table column cell and create the binding.

    All bindings of subview’s in the cell are made to the objectValue.

    Configure the textField instance’s value binding as shown Table 5-3.

    Table 5-3  Table Cell View’s NSTextField instance’s Value binding settings

    Binding Field

    Value

    Bind to:

    Table Cell View

    Model Key Path

    objectValue.name

  7. Select the image view in the table column cell and create the binding to the Value binding.

    All bindings of subview’s in the cell are made to the objectValue.

    Configure the imageView instance’s value binding as shown in Table 5-4.

    Table 5-4  Table Cell View’s NSImageView instance’s Value binding settings

    Binding Field

    Value

    Bind to:

    Table Cell View

    Model Key Path

    objectValue.image


Did this document help you? Yes It's good, but... Not helpful...