Learn Objective-C on the Mac - Mark Dalrymple [47]
You’ll learn how to use a table view by extending the VillainTracker application from Chapter 4. The new version of VillainTracker we create in this chapter will maintain an array of villains, display them all in a table, and let the user edit all the attributes of the selected villain when they click on its entry in the table. We’ll start by using Xcode to extend the AppController class’s interface to include an array of villains, some new outlets for connecting to the new table view and the window itself, and action methods for adding and deleting villains. Then we’ll prepare the GUI layout in Interface Builder, and then we’ll go back to Xcode to implement the changes to our controller’s implementation for handling the table. Figure 5-1 shows you the end-result we’re shooting for.
Preparing AppController for Multiple Villains
In Xcode, open the project you created in Chapter 4, and navigate to AppController.h so we can update the class’s interface to accommodate our upcoming changes. First, we’ll add the new instance variables we need. Because we’re going to maintain a list of villains, we’ll create an NSMutableArray called villains to contain them all. We also add an outlet called villainsTableView in order to access the NSTableView where we’re going to present the list of villains. While we’re at it, we’ll also add an outlet called window for connecting to the NSWindow containing all of our GUI components. We’ll put this to good use a little later.
For the sake of completeness, we also add a @property declaration for the new villains instance variable, to allow other code (including code in AppController’s implementation) to easily and safely access and change this value.
Figure 5-1. The completed app window
We’ll also add declarations for newVillain: and deleteVillain:, our two new action methods. The following code listing shows the state of AppController.h after making these changes (new lines are in bold):
Now complete the @property definition of villains by adding a matching @synthesize in AppController.m. You’ll also need to add two new method implementations (just empty shells for now) for the new action methods. Add these lines to the @implementation AppController section:
Now we’ve added all that we need to AppController’s interface, and some empty stub methods to the implementation. Hit the Build button just to make sure it compiles cleanly, and we’ll move on to adjusting the GUI to make room for the table.
Making Way for the Table View
In Xcode’s project navigation panel, double-click MainMenu.xib to open it in Interface Builder. You’re going to make the window bigger, add a table view and a few buttons, and adjust the resizing characteristics of all the NSBoxes so that the table view will resize fully in both dimensions, and the other boxes will move accordingly.
Start by resizing the window, making it about 400 pixels wider (about half again as wide as it was to begin with), but the same height as before. We’re going to follow the conventions of the western, left-to-right world, and arrange things so that the selection on the left (in the table view) determines what is presented on the right (all the other views), so you should also drag all the existing views to the right side of the window. See Figure 5-2 to get an idea of what to aim for.
Now bring up the Library window and type “tableview” into its search field. Drag the resulting NSTableView into to the empty space in the window. The default size for the table view is quite a bit smaller than the space you’re putting it in, but don’t bother with resizing it to fill the available space just yet, we’ll get to that in a bit. For now, let it just sit there in the middle of the empty space.
Figure 5-2. Make your window look like this in preparation for the table view.
The first thing we’re going to do with this table view is configure its