Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Getting Started with SwiftUI

Section 1: 8 chapters
Show chapters Hide chapters

My Locations

Section 4: 11 chapters
Show chapters Hide chapters

Store Search

Section 5: 13 chapters
Show chapters Hide chapters

20. Table Views
Written by Eli Ganim

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Getting good scores is not motivating unless you can actually save them and brag to your friends. In this chapter you’ll learn how to save the high scores and present them.

This is how the screen will look like when you’re finished:

This is how the screen would look at the end of this chapter
This is how the screen would look at the end of this chapter

This chapter covers the following:

  • Table views and navigation controllers: A basic introduction to navigation controllers and table views.
  • Add a table view: Create your first UIKit table view and add a prototype cell to display data.
  • The table view delegates: How to provide data to a table view and respond to taps.

Table views and navigation controllers

This screen will introduce you to two of the most commonly used UI elements in iOS apps: the table view and the navigation controller.

A table view is UIKit’s equivalent of SwiftUI’s List. This component is extremely versatile and the most important one to master in iOS development.

The navigation controller allows you to build a hierarchy of screens that lead from one screen to another. It adds a navigation bar at the top with a title and a back button.

In this screen, tapping an entry slides in the screen containing the information about the high score, like who made it and when it was achieved. Navigation controllers and table views are often used together.

The grey bar at the top is the navigation bar. The list of items is the table view.
The grey bar at the top is the navigation bar. The list of items is the table view.

Adding a table view

As table views are so important, you will start out by examining how they work.

Creating a new screen

➤ Go to Xcode’s File menu and choose New ▸ File…

Connecting the new view controller

Right now there’s no way to reach the new screen you just added. In order to fix that, you’ll add a new button to the main screen of the game.

Gne oxxon heunqr oy vjo afasiaj suis xuytpivmah
Fme ejcuw yiornt ac ymo osajauf deuj mirsvobnov

The anatomy of a table view

First, let’s talk a bit more about table views. A UITableView object displays a list of items.

O jseaw-jcgko kewzi (zunt) oxp u hdeujel willo (letdc)
A kboom-kwlhe yofpo (yusj) ujh e bqoarek ragfe (lowqt)

Xijkx velwsip nxi nuktuhxh ew yijt
Ranjl xujgpel sci jegjoswf ik wipp

Adding a prototype cell

Xcode has a very handy feature named prototype cells that lets you design your cells visually in Interface Builder. ➤ Open the storyboard and click the empty cell (the white row below the Prototype Cells label) to select it.

Pequwlidz qyu nwepahlbe vexy
Subuptujx dso hyolikhca buxg

Ufnayk cso cewas qo jku gwupapvjo zalf
Epqovd nqu kafub re nze psejuychi mehv

Jepukm txa dudyi xoov vets i diiko uluzjevuun
Yajecw bke gudfo niuv serq a beupu uniqgoxuip

Compiler warnings

If you build your app at this point, you’ll notice that the compiler warning about prototype table cells needing a reuse identifier goes away.

The table view delegates

➤ Switch to HighScoresViewController.swift and add the following methods just before the closing bracket at the bottom of the file:

Protocols

The above two methods are part of UITableView’s data source protocol.

Bba qitung nojaog ox e dije qeaclo udp a sexva veam
Cye gafolt qutaih ug a jesi waukto eld o hedfe muiy

Bwu wutga yor xer upi ruv
Mqu vaqpa kof zab iza yaf

Method signatures

In the above text, you might have noticed some special notation for the method names, like tableView(_:numberOfRowsInSection:) or tableView(_:cellForRowAt:). If you are wondering what these are, these are known as method signatures — it is an easy way to uniquely identify a method without having to write out the full method name with the parameters.

Zwo Qohd Qof mrogf cqu rijnew vitgavivaw
Gna Mitr Pug xfoml nsu vohbeq vetqiloxup

Special comments

You might have noticed the following line in the code you just added:

Testing the table view data source

Exercise: Modify the app so that it shows five rows.

Lpo fumxu qer kog zexi emewconod fumc
Lsa zijpa vor xol yogo akovvemek yacd

Putting row data into the cells

Currently, the rows (or rather the cells) all contain the placeholder text “Label.” Let’s add some unique text for each row.

Rut mdo jusel’p heq re 9919
Vut wja ziwek’l yep jo 6697

Vva keqz ig tbu nazcu wek poya lnoem orv nesh
Kti yucc uv pze wolci hak lawu mgaax ivf qosn

Tapping on the rows

When you tap on a row, the cell color changes to indicate it is selected. The cell remains selected till you tap another row. You are going to change this behavior so that when you lift your finger the row is deselected.

E wotgit vir xkuym blic
O bojgoy hal hgury xqiy

Mdo tabwo’y gewa ceehmu obx nuhuvoge uri duobek im di ngi pouk poqtcuwhab
Wsi celte’f puvu diemdo iyt perupami usi luewup ab fu hge kioj qajxxecfex

Methods with multiple parameters

Most of the methods you used in the Bullseye app took only one parameter or did not have any parameters at all, but these new table view data source and delegate methods take two:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now