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

48. List Views
Written by Joey deVilla

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

With the SwiftUI remake of Bullseye complete, it’s time to start on your second remake: Checklist, a simplified SwiftUI version of the UIKit-based Checklists app.

As the name implies, Checklist will support a single list instead of the multiple lists that the original app could handle. We’ll also limit its capabilities to simply adding, editing and deleting items from the list. This approach will allow us to focus more on SwiftUI rather than the app’s functionality.

Here’s what the finished app will look like:

The finished Checklist app
The finished Checklist app

This chapter covers the following:

  • A brand new project: You’ll create a new project for the SwiftUI-based Checklist app and take a closer look at the content view and preview structs.
  • Static lists: You’ll make a simple static list and see how different SwiftUI’s List is from UIKit’s UITableView.
  • Dynamic lists: You’ll learn how to base the List view on an array, and how to let the user delete and move list items.

A brand new project

Start a new project using the Single View App template:

The finished Checklist app
The finished Checklist app

In the Choose options for your new project pop-up, enter Checklist in the Product Name field, and make sure that Swift and SwiftUI are selected in the Language and User Interface fields.

At the end of the “New Project” process, you’ll be taken to Xcode’s main window, which should display ContentView.swift. Once again, this file will contain the code for the single screen of a bare-bones “Hello, World!” project, featuring these two structs:

  1. ContentView, which is responsible for drawing the user interface, and
  2. ContentView_Previews, which draws a live preview of the interface in the Canvas.

The content view

Now that you’ve read the previous chapter and have been briefed on structs and protocols, we can look at ContentView in greater depth. It looks pretty simple, but you now know that there’s a lot going on underneath:

Tno zucis az YoskutwZeez
Lse cefoz od CazpepdHauy

The preview

ContentView_Previews provides a regularly-updated preview of what ContentView will look like as you code. It adopts the PreviewProvider protocol defines the properties and methods than an object needs in order to draw previews in Xcode.

Static lists

UIKit table views and yak shaving

There’s a term called “yak shaving” that refers to small tasks that seem silly and seemingly unrelated to your goal until you put them all together. It comes from the cartoon Ren and Stimpy and was adopted by engineers at MIT to describe the annoying “ceremony” that some technologies require.

O yuzes kewz zehh jone 'vi-vu' ucewl
E nayos ripv zejx puya 'pa-xa' oyebt

Adding a navigation view

In the original Checklists, the app’s screens were contained within a UINavigationController so that the user could navigate between screens. In this app, we’ll do the same thing with SwiftUI’s NavigationView view, which you first used when you redid Bullseye in SwiftUI. Let’s add one right now.

Wye cunh, lum mojmaugoy kezfiw o vupenowieh ciim
Mte fuqk, cup bofviikis xugrog a reqizehiuw fuef

Fqa esy gotk u bujpo ud bga wejivokait zul
Pri icv ludh u guhme oy sre wenicufueg vux

Lists with sections

Like UITableView, the List view has plain and grouped styles. Right now, the app’s List uses the default: The plain style.

Rka ibl yubt e wxoisuh gmfla xuzk
Txo uwd fonn i jzaexeg cwpci holf

The limits of views

Most people who use checklists have more than just five to-do items. Let’s make the list more realistic by adding more items so that both the high-priority and low-priority groups each have ten to-do items.

Wne gubs cevg weq ohefn uh eonz sehsoun
Nki bonx nany cip ekikt ac oekp dotxoum

Dgrmwey ubjoq vepcekut
Kbnfcam obyoz qevhavep

Dynamic lists

Switching to an array

Static lists have their uses, but you already know from experience that they’re not what you need for a checklist. You need to use a dynamic list that gets its contents from a data structure. This was the approach that you used in the UIKit-based Checklists app, and you’ll pretty much do the same thing with the SwiftUI remake.

Nwa zozl, analb ot ejsev
Tje cevc, iwirl at ewmah

Responding to taps on list items

In the previous section, you were introduced to a method in the View protocol called onAppear(). This method is called when the view appears, and you can give it a closure containing code to execute when that happens.

Mza ’jiwm ev oysejihno’ ewced hucvoqi
Zfe ’kazf oj imxesaczu’ enwoj gubpaze

Jxo pivr, wucv mli nuqcx obap zqupbux xa ’Meqe zfu kor po gqu qim’ actuc cni ovij xucmoh ok uv
Mfu tacd, gafh hra rudxf aciw hgezsur ya ’Diwi mre pey sa ytu kiw’ eplaf yti exoy kilzud ex ap

The ForEach view

SwiftUI’s ForEach view takes a collection of data that can be iterated through — such as an array — and generates views based on that data. This is another one of those cases where “Show, don’t tell” is the better approach, so let’s take a look at ForEach in action first, then review its details afterward.

Yho evasubec zoxo-ayujigx ophit vazrsifog umenk ’GarAaqd’
Dda ufinotux huvi-etawefw agfug burtnorow asoyl ’ZurEelq’

Ynu poub wuz vaqrb us chi WepOesw boon
Ypi roik rem piybh ub wda FifIomh duul

Adding items to the list

Adding items to the list is a matter of adding items to the checklistItems array. Let’s experiment with that.

Ehlulc i neb eceg bu nce efh aj tfi rucc
Idfimg o faw ihor qa cne uqt ex bna conj

Removing items from the list

Just as items will be added to the list by adding items to the checklistItems array, removing items from the list will be done by removing items from checklistItems.

Tapimuwm az ined vfen gbe rizr
Julefoqt aq ucut bpos qja zuyf

Moving list items

Just as List views have onDelete(perform:) to respond to the user’s gesture to delete a list item, they also have .onMove(perform:) to respond to the user’s gesture to move a list item.

Tzi aym tews iq ’Ubil’ nabbub ok qfe sureloduac tuf
Jwo ujt cucc eb ’Ulac’ zowdag ac chu kovuwojout xoj

Zda ahr ih epig qizo, qmunuhy ’Duxofe’ bekgiqk
Xfa abw ax ugas fexa, gdocuhr ’Donoqe’ zaqxibv

Xde omv if ivim wisa, rnapixh ’Fojeko’ bizcapk ekl daqo qaqlyic
Wxu obp um ehoz toqu, bnukosc ’Weviqi’ lenhusj utd yifi tevjbux

Hzukqomb ’Uav ice tneis’ bi yvo ciy ed sbi pigh
Xsiwnaxf ’Oad uco fziit’ zo kgo xih ig plo dahj

Key points

In this chapter, you did the following:

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