Swift For Dummies
Book image
Explore Book Buy On Amazon

The Master‐Detail Application template in Swift, and thus the Locatapp, uses a UITableViewController to display the master list of events. Table views are very common in Cocoa Touch and, on the Mac, in Cocoa. A great deal of the work is done for you already, and two protocols play a key role in the structure.

Table views on both OS X and iOS integrate very well with Core Data; the combination is frequently used as it is in Locatapp. This excursion into the frameworks helps to familiarize you with real‐world uses of protocols and delegates.

The Master‐Detail Application template contains the protocols that you need. They are already configured for you in the template, but you should examine what you have.

The basic architecture is that UITableViewController is designed to work together with two protocols: a data source (a class that adopts the UITableViewDataSource) and a delegate (a class that adopts the UITableViewDelegate protocol).

The data source provides the functionality involved with the table and its data. Its required methods specify the number of rows and sections in the table along with their titles and headers; its methods also manage editing of the table structure (moving and deleting rows).

The table view delegate protocol handles the appearance of the table: indentations, row heights, selection, editing table content (as opposed to the structure which is handled by the data source protocol), and taps in a cell, and the appearance of the contents of a cell.

Looking at delegation and protocols

The delegate protocol provides the user interface. Its methods manage selections and the editing of content.

Together, UITableViewController and its protocols (UITableViewDataSource and UITableViewDelegate) provide a powerful set of functionality that is easy for you to use and customize. This could all have been written as one gigantic class, but by splitting it into a main class and two protocols, it is easier to maintain (and, for many people, easier to understand).

The common implementation in many examples and Xcode templates basically reassembles the base class and the protocols into one large object.

Setting delegates in Interface Builder

You can assign an instance of a class to the delegate property in UITableView. You don’t have to worry about that because first of all, that’s a framework/interface issue and this book focuses on the Swift language, and second of all, it’s already done for you in most of the templates and examples. Here’s a review of how it’s done.

  1. Open Main.storyboard in Xcode using Interface Builder (the default editor for that file).

  2. Open the document outline if needed.

  3. Open Master Scene, the Master controller (yellow circle), and then Table View.

    There will be two Master Scene sections in the document outline. Open each one and then look at the Master controller in the yellow circle. One has a navigation arrow, and the other has a table list image. You want the table list image.

    image0.jpg

    When you look at the Table View in Main.storyboard using Interface Builder in Xcode and shown here, you’ll see that the two declarations shown previously (delegate and dataSource) show up as outlets. They are connected to the Master object in the document outline rather than being connected in your code.

  4. Select Master (the yellow circle).

    As you can see, when you select Master in the document outline of Interface Builder, you can see the other side of the connection: the two referencing outlets (dataSource and delegate) are connected to Table View.

    image1.jpg

    When you make connections like these in Interface Builder, you can always look at it from both sides. It is this connection in the template that associates the table view with Master.

  5. With Master selected, look at the Identity inspector in the utilities area (shown at the back right of the preceding figure).

    As you see, Master is an instance of MasterViewController. You can see that by highlighting it and looking at Quick Help. There you’ll see that MasterViewController is a subclass of UITableViewController.

  6. Look in MasterViewController.swift to see its declaration in the template (and, thus, in Locatapp).

    There’s no reference to the protocols. How do they get into the code?

    The answer is that MasterViewController is a subclass of UITableViewController.

    image2.jpg
  7. Highlight UITableViewController in the declaration shown and open Quick Help.

    There you can find a link to its reference.

  8. If you click on the reference to the reference documentation for UITableViewController.

    You’ll see the documentation for UITableViewController as shown.

    image3.jpg

    UITableView­Controller conforms to both UITableViewDataSource and to UITableViewDelegate. This means that it or subclasses of it must conform to those protocols. (It really doesn’t matter whether it is the original class — UITableViewController in this case — or a subclass such as MasterViewController that conforms. The required methods and properties must be present when you build the project and run it.

About This Article

This article is from the book:

About the book author:

Jesse Feiler is a developer, consultant, and author specializing in Apple technologies. He is the creator of Minutes Machine for iPad, the meeting management app, and Saranac River Trail and is heard regularly on WAMC Public Radio for the Northeast’s The Roundtable.

This article can be found in the category: