Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [48]

By Root 962 0
columns. By default, a new table view has two columns, but we’d like it to have three, so that we can show the name, date of last sighting, and mugshot for each villain in our list. To add a column, you have to sort of “drill down” through a few layers of views. Open the Attributes Inspector to help guide your way; it will always show you a bit of info about the selected object, most critically the class name. The first time you click on the table view in your window, you’ll see that the Attributes Inspector shows you “Scroll View Attributes.” That’s because the table view you dragged out from the Library is actually contained inside an NSScrollView. Click again, and the inspector will show “Table View Attributes;” you’re getting closer! Click again, this time in the large blank space of the table (below the column headers) and the inspector will show “Table Column Attributes,” which is what we were looking for. Now that you have a column selected, Choose Edit➤Duplicate from the menu, and you’ll see that your table now has three columns.

We want to configure these table columns to correspond to the name, lastSeenDate, and mugshot values in our villain objects, and we’ll do that by supplying each of them with an identifier that is the same as the key-name for the attribute we want to show. Later on, you’ll see how we use this identifier in our code to easily prepare values for display. Select the first column, and then in the Attributes Inspector, type “name” into the Identifier field. While you’re there, type “Name” into the Title field; this determines what will appear in the text of the column’s header. Repeat this process for the second column, entering “lastSeenDate” as the Identifier and “Last Seen” as the Title, and again for the third title, entering “mugshot” for the Identifier and “Mugshot” for the Title.

That final column needs a little extra configuration, because it’s going to display an image instead of text. Fortunately, Cocoa provides a class that makes this easy. You may remember from Chapter 4 that whenever we had an NSMatrix full of controls, those controls were actually instances of a subclass of NSCell, a class that is more lightweight than NSView or NSControl. NSTableView also draws its content using cells, and by default it uses NSTextFieldCell for displaying and editing string values. We’ll change this to make it use an NSImageCell instead, so that the third column can display an image. Once again go to the Library window, and enter “imagecell” into the search field, which will bring up NSImageCell. Drag this over to your table view, into the space where the mugshot column should be. The column will highlight and your mouse pointer will acquire a green “+” badge when you’re over a drop-ready spot. Release the button, and voila! That table column is now ready to display an image.

Now we’re going to add a pair of buttons, one for adding a new villain to the list, and one for deleting the selected villain. Back in the Library window, type “button” into the search field, and you’ll see a surprisingly large list of buttons show up in the results. Someone at Apple really likes to design buttons! Despite their differences in appearance, just about everything in this list is just an NSButton, which at its core always does the same basic thing. Some of them are pre-configured in different modes to work as a checkbox or disclosure triangle, but otherwise they all do the same basic thing: call an action method in a target object when clicked.

Pick one of the first few buttons you see in the list (maybe Push Button or Rounded Rect Button) and drag it to your window, dropping it just below the table view. This will be our “Add Villain” button. Instead of titling the button with the text “Add Villain” however, we’ll use one of Cocoa’s built-in graphic images to make it resemble “add” buttons in other applications. While the button is still selected, bring up the Attributes Inspector, and locate the Image combo box. Click on the small triangle at the right end of the combo box, and start typing “nsaddtemplate.

Return Main Page Previous Page Next Page

®Online Book Reader