Cocoa Programming for Mac OS X - Aaron Hillegass [62]
Figure 11.5. Drop an Image & Text Table Cell View
Now drop a number formatter (from Cocoa->Controls) onto the NSTextField in the Price column (Figure 11.6). In the Attributes Inspector, use the 10.4+ formatter, and set the style to Currency.
Figure 11.6. Configure Formatter
The third column will be populated by check boxes, so select the NSTextField in the third column’s cell view and delete it. Make sure that you have selected and deleted the Static Text - Table View Cell, and not the Table Cell View itself. This will take two clicks directly on the cell.
Now drop a Check Box (from Cocoa->Data Views)—not the Check Box Cell—in place of the text field in the third column. Select the check box and clear its title (Figure 11.7).
Figure 11.7. Drop Check Box Control
Below the table view, drop an NSDatePicker, two buttons, an NSImageView (which is called Image Well in the Library), and an NSLevelIndicator onto the window. Label the buttons Add and Remove. Put label text fields next to the date picker and the level indicator. The labels should be Date Purchased and Condition. Make the NSImageView editable by using the Attributes Inspector (Figure 11.8).
Figure 11.8. Attributes of the NSLevelIndicator
In the Attributes Inspector of the NSLevelIndicator, set its max to 5 and its min to 0. Set its style to Rating mode (to get the stars). Also, make the level indicator editable (Figure 11.8).
Select the date picker, the image view, the two labels, and the level indicator. Using the Editor -> Embed -> Box menu item, wrap them in a box (Figure 11.9).
Figure 11.9. Embed in a Box
Connections and Bindings
Now you are going to do a bunch of bindings. We will walk you through it step-by-step. Figure 11.10 is a diagram of the bindings that you are going to create between your views and the array controller.
Figure 11.10. Summary of Bindings
If you have used cell-based table views in the past, you will find the bindings on view-based table views to be much more straightforward. With view-based tables, you will bind the table’s contents to the array controller; then the values of the controls you place within the cell views, not the table column, will be bound to the object value for that row. A reminder: In this book, you will never bind a scroll view or a cell.
Start by binding the table view’s Content binding to Cars’ arrangedObjects. Leave the Model Key Path empty. Remember, Cars is the NSArrayController. Next, bind the Selection Indexes to Cars’ selectionIndexes. See Figure 11.11.
Figure 11.11. Table View Bindings
Now bind the value of each column’s cell view control(s), as shown in the following table.
Note the pattern in the bindings. Each control’s value is bound to a particular property of the table cell view’s objectValue. Recall that the table’s content is bound to the NSArrayController’s arranged objects. Each of those objects is an instance of our Car entity. When the table view’s contents are bound, the objectValue property of the table cell view (NSTableCellView) is populated with the entity instance for that row. Once we understand this, we can configure very straightforward bindings, such as those in the table.
Before continuing, use the Attributes Inspector to set the Behavior of each Static Text Field to Editable.
Make the Add button trigger the add: method of the array controller (Figure 11.12).
Figure 11.12. Set Target and Action of Add Button
Make the Remove button trigger the remove: method of the array controller.
Bind the value of the controls to the selection of the array controller as follows:
Bind the value of the image view to Cars. Choose the controller key selection and the key path photo. Also, check the box that says