Make a Place to Play

It’s time for another project. Copy the steps from Create a Place to Play with Tests, but use TableView as the project name. Delete its initial test file as usual.

In the Project Navigator, select the TableView group. Create a new file, selecting Cocoa Touch Class, and make it a subclass of UITableViewController.

With this new file in place, delete the old file ViewController.swift.

Next, let’s put this in the storyboard. Edit Main.storyboard, select “View Controller Scene,” and press to delete it. Select View Libraries Show Library from the Xcode menu or press Shift--L to bring up the Object Library. Double-click “Table View Controller” to add it to the storyboard. Now let’s modify it. In the storyboard, select “Table View Controller Scene.” Then in the Xcode menu, select View Inspectors Show Identity Inspector or press --3. In the Identity Inspector on the right, in the Custom Class section, click the Class pull-down menu. Select the class we just made, TableViewController, like you see in the image.

images/table-view/class-pull-down.png

Show the Attributes Inspector by selecting View Inspectors Show Attributes Inspector from the Xcode menu or press --5. In the View Controller section, shown here, select “Is Initial View Controller.”

images/table-view/initial-view-controller.png

Xcode still shows a warning symbol. Click on the symbol, or select View Navigators Show Issue Navigator, or press -5. Under “Unsupported Configuration,” click the warning that says the following:

 Prototype table cells must have reuse identifiers

This will select “Table View Cell.” In the Attributes Inspector on the right, in the Table View Cell section (shown in the following image), enter cell as the identifier.

images/table-view/reuse-identifier.png

Before we work on the code, let’s make sure this shows an empty table. Set your destination to an iOS simulator, then press -R to run the app. You should see a table with empty rows.

Now delete the generated contents of the TableViewController class. Define the following property, which will serve as our simple model:

TableView/TableView/TableViewController.swift
 private​ ​let​ model = [
 "One"​,
 "Two"​,
 "Three"
 ]

Add these methods to display this model:

TableView/TableView/TableViewController.swift
 override​ ​func​ ​tableView​(_ tableView: ​UITableView​,
  numberOfRowsInSection section: ​Int​) -> ​Int​ {
  model.count
 }
 
 override​ ​func​ ​tableView​(_ tableView: ​UITableView​,
  cellForRowAt indexPath: ​IndexPath​)
  -> ​UITableViewCell​ {
 let​ cell = tableView.​dequeueReusableCell​(
  withIdentifier: ​"cell"​, for: indexPath)
  cell.textLabel?.text = model[indexPath.row]
 return​ cell
 }

And when a row is selected, we’ll print the selected row to the console:

TableView/TableView/TableViewController.swift
 override​ ​func​ ​tableView​(_ tableView: ​UITableView​,
  didSelectRowAt indexPath: ​IndexPath​) {
 print​(model[indexPath.row])
 }

Let’s make sure this table works. Run the app. You should see three rows, showing “One,” “Two,” and “Three.” Select View Debug Area Activate Console or press Shift--C to show the console on the bottom right. Tap each row to confirm that the app prints the selected row in the console.

Now that we have a table view, let’s see how to write tests for it. In most cases, all we need to do is test that each delegate method does what we want.