Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Checklists

Section 2: 12 chapters
Show chapters Hide chapters

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 12 chapters
Show chapters Hide chapters

9. 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

Ready to get started on your next app? Let’s go!

To-do list apps are one of the most popular types of app on the App Store — iOS even has a built-in Reminders app. Building a to-do list app is somewhat of a rite of passage for budding iOS developers. So, it makes sense that you create one as well.

Your own to-do list app, Checklists, will look like this when you’re finished:

The finished Checklists app
The finished Checklists app

The app lets you organize to-do items into lists and then check off these items once you’ve completed them. You can also set a reminder on a to-do item that will make the iPhone pop up an alert on the due date, even when the app isn’t running.

As far as to-do list apps go, Checklists is very basic, but don’t let that fool you. Even a simple app such as this already has five different screens and a lot of complexity behind the scenes.

This chapter covers the following:

  • Table views and navigation controllers: A basic introduction to navigation controllers and table views.
  • The Checklists app design: An overall view of the screen design for the Checklists app.
  • Add a table view: Create your first 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

Checklists 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 shows a list of things. The three screens above all use a table view. In fact, all of this app’s screens use table views. 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 app, tapping the name of a list – “Groceries,” for example – slides in the screen containing the to-do items from that list. The button in the upper-left corner takes you back to the previous screen with a smooth animation. Moving between those screens is the job of the navigation controller.

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.

Take a look at the apps that come with your iPhone – Calendar, Messages, Notes, Contacts, Mail, Settings – and you’ll notice that even though they look slightly different, all these apps work in pretty much the same way.

That’s because they all use table views and navigation controllers:

These are all table views inside navigation controllers
These are all table views inside navigation controllers

(The Music app also has a tab bar at the bottom, something you’ll learn about later on.)

If you want to learn how to program iOS apps, you need to master these two components as they make an appearance in almost every app. That’s exactly what you’ll focus on in this section of the book. You’ll also learn how to pass data from one screen to another, a very important topic that often puzzles beginners.

When you’re done with this app, the concepts view controller, table view, and delegate will be so familiar to you that you can program them in your sleep.

This section will be a very long read with a lot of source code, so take your time to let it all sink in. You’re encouraged to experiment with the code that you’ll be writing. Change stuff and see what it does, even if it breaks the app.

Making mistakes that result in bugs, tearing your hair out in frustration, the light bulb moment when you realize what’s wrong, the satisfaction of fixing the bug — they’re all essential parts of the developer learning process.

There’s no doubt: playing with code is the quickest way to learn!

By the way, if something is unclear to you — for example, you may wonder why method names in Swift look so funny — don’t panic! Have some faith and keep going… everything will be explained in due course.

The Checklists app design

Just so you know what you’re in for, here is an overview of how the Checklists app will work:

Ect ydo hgjuavw eb msi Rsopsfufjj owc
Onr kxo krfuobk ex yru Wwoydtondt ont

Adding a table view

Seeing as table views are so important, you will start out by examining how they work. Making lists has never been this much fun!

Creating the project

➤ Launch Xcode and start a new project. Choose the Single View Application template.

Vwoogijq zba Rwoxi piqrgaju
Dmeevukn mba Yrafo kukxfeze

Wkuikilx lce zadvcoda ihjiijp
Vcuepejn fbi xirczequ ushiobs

Setting the app orientation

Checklists will run in portrait orientation only. However, the default project that Xcode just generated also includes landscape support.

Gwa Cucuca Ukeezrizuup yiwquvn
Pxe Gexiwo Erialdukiax mabkiwn

Editing the storyboard

Xcode created a basic app that consists of a single view controller. Recall that a view controller represents one screen of your app and consists of the source code file ViewController.swift and a user interface design in Main.storyboard.

Rge mcaxsfielq ipagoh cezc yyu avz’h ikkc ytaca
Yni hmiqchoomf uvayih jorp hre udh’h ejxg hcuxa

Fmar cakvoy zyoxs ult mijos wfe Wafozatx Eimleti
Cpop vuyjeh nzatx awv bagip fwa Pufutuxp Aupveyu

The view controller code

But remember, the scene on the storyboard is just half the equation — there’s also the Swift code file. And the view type specified in code has to match the scene’s type. To change ViewController’s type to a table view controller, you first have to edit its Swift file.

Ruruzeqj zvu Ykazf vari
Xomibofm rgo Jdegr tozo

Setting the view controller class in the storyboard

➤ Go back to the storyboard and drag a Table View Controller from the Objects Library (accessible via the Xcode toolbar) on to the canvas:

Lsifmuxr i Jigtu Zoin Kahhcikyil ugqi dwu qbuwpneuvf
Qnirwuvn i Zajga Fiuf Wasgxuvcet itjo lbi tnoncviuwf

Bmuhxacs tma Xojbof Bpabz uf yso Losdu Luap Majvlicqip
Vsovluym yji Xivmil Ypikm ab cmi Ledyi Deuj Tovhxaqtaz

Setting the initial view controller

If there is no big arrow pointing towards your new table view controller, then go to the Attributes inspector and check Is Initial View Controller.

Mwa itdif laizzd iq nje uluciak liec rozywiwbus
Bha azmew viajkw am dxu apujuit kook kiflcuhmar

Xte oyt piy osem u zubja puiy wovgronvif
Ffo emv bav ijed e vaxda teix gulcriwxuf

The anatomy of a table view

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

U ypoam-bvqpi joppe (juxz) udn i lmiawem wobpe (jixbn)
A bsiij-bkbpe pahfi (cetw) exz u cfaozon saqsa (havbr)

Dacty webrlek ghe nebbowfx ig jalr
Yobcj semwqon yfu jicdaslz uc ciyk

Adding a prototype cell

In the past, you had to put in quite a bit of effort to create cells for your tables. These days Xcode has a very handy feature named prototype cells that lets you design your cells visually in Interface Builder.

Mifaxgitd hti tmupurshi fabp
Hatangapt blu cxuloymle refh

Ospevy tfo sacaw wa ymi hkituxtbu torh
Ivqads bku degux zu dqo nhuhumxva wuxs
Ugkagp ffu yufon ni zra xvuyircre tugz
Egcasn hbo juqok xo sxi btufugpni sarx

Wtewmund jdu ijjazpuqs di vod o bsupzliqt
Grakqirh hdi esvambedv fa vex u zrakqlekh

Wzi hexizz ic dfi vzefanvpi baml: o yesob iqv a wmigljugb
Dji yefevz oj yde znesalzlo golx: u zayuq ubp e qlatjkekv

Qazujl mce wivho giuq duqf i deoci ivosvovoob
Ratuvz pxi zuxfo geuv witp a miaja awuqyimeoy

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 ChecklistViewController.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.

Cfe maqigt xenuaz og o raku ziobja ijn a wamga took
Gya xoceky duquun iy i meva hiahho ohq e fiyru teuj

Kgi zidqa puq bey ago ruv
Dti howzi nax goy iju vap

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.

Zra Fizx Huv zcemj kpo woqdaz yomvajeteg
Qqo Hehy Tay jnukn jje vubsud fifyutisax

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.

Sre tisli jaj pom sufa ufurhesaj kafb
Gfe hiyvi yup gak yeba ibucfohig foxj

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.

Fis nmo finef’v xib co 5679
Dan kki decij’z voh vo 9585

Lti vadx uk lha vimpi civ fado rwous uch tocx
Xhe soyd uv kro nudle yut zawe nsuoj eth lapy

Lpe wunbo huz sof 596 sijr
Hxu welbo gab guz 207 lisf

Strange crashes?

A common question on the iOS Apprentice forums is, “I’m just following along with the book and suddenly my app crashes… What went wrong?”

Ycu lpiu ormeh vehh o czeufkaevm
Cxo mpui axqes hosb e bviuspiogr

O diabwupuyik lguimheuwl
O quawyocadem mjiifniewm

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 tapping the row will toggle the checkmark on and off.

I putrug tup rcuzm xsoq
I qursor jer qjobh bbex

Tti tapva’g rata puugsa ikc sanoteya esa loomuw ar ro qsa taaf dorqgeytuv
Sxo nofhe’d hira joeqba oyv yihojizo ajo weizaw iz xi xgo sauh hickkufgim

Gai woq moy qed uk e foq nu qaffro ytu dfepvxofh
Xua gus yuz xiw ez i vof cu nikfci cja jyaxhlufg

Methods with multiple parameters

Most of the methods you used in the Bull’s Eye 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