Binding a View-Based Table View to a Core Data Entity

October 7th, 2014

Filed under: Cocoa, Mac Development | 3 comments

My main reason for writing this post is to have a reference for myself in case I need to bind a view-based table in the future. If this post helps anyone else, that’s a bonus.

Set up the Array Controller

The first step is to create an array controller and configure it to control a Core Data entity.

  1. Add an array controller to the xib file.
  2. Select the array controller from the object list and open Interface Builder’s attributes inspector.
  3. Choose Entity Name from the Mode menu.
  4. Enter your entity name in the Entity Name text field.
  5. Select the Prepares Content checkbox.

CoreDataBindingsObjectController

Bind the Array Controller to the Managed Object Context

After setting up the array controller, you must bind it to your Core Data managed object context.

  1. Open the bindings inspector.
  2. Click the disclosure triangle next to the Managed Object Context binding.
  3. Select the Bind to checkbox.
  4. Choose File’s Owner from the menu.
  5. In the Model Key Path text field, enter managedObjectContext.

CoreDataBindingsBindToManagedObjectContext

Bind the Table View

The table view needs data to display. Bind the table view to the array controller to get the data.

  1. Select the table view from the object list.
  2. Open the bindings inspector.
  3. Click the disclosure triangle next to the Content binding.
  4. Select the Bind to checkbox.
  5. Choose the array controller from the menu.
  6. In the Controller Key text field, enter arrangedObjects.

CoreDataBindingsBindTableView

If you have problems with the table view selection, use the table column’s Value binding instead of the table view’s Content binding. In the code I’m working on, I have a table view and a text view. Selecting an item from the table view fills the text view with the item’s contents. When I used the table view’s Content binding, the text view’s contents didn’t change when I selected an item in the table view. Using the table column’s Value binding fixed the problem.

Bind the Table View Cell

View-based table views require you to bind the table view cell to display something in the table view.

  1. Select the table view cell from the object list. You may find it easier to use the jump bar to locate the table view cell.
  2. Open the bindings inspector.
  3. Click the disclosure triangle next to the Value binding.
  4. Select the Bind to checkbox.
  5. Choose Table Cell View from the menu.
  6. In the Model Key Path text field, enter objectValue.attribute, where attribute is the attribute in your Core Data entity that you want to show in the table view.

CoreDataBindingsBindTableViewCell

Tags:


3 thoughts on “Binding a View-Based Table View to a Core Data Entity

  1. Con says:

    THIS is just perfect, particularly the comment about binding to the column’s Value binding. Thanks.

  2. Mark Cowan says:

    Thank you so much for posting this—it was a lifesaver. But using the most recent version of Xcode, I had to change one step above to get my project to work.

    Under “Bind the Array Controller to the Managed Object Context,” I had to choose “Delegate” instead of “File’s Owner.” I’m just curious: does “File’s Owner” still work for you?

    • Mark Szymczyk says:

      Mark,

      I’m guessing you have an application that is not document-based.

      In the article, I was creating a document-based application, and you can’t bind the array controller to Delegate in a document-based application. In a side project I’m working on that uses Core Data in a shoebox (not document-based) application, I bind to Delegate because binding to File’s Owner raises an exception.

Leave a Reply

Your email address will not be published. Required fields are marked *