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

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

Ready to get started on your next app? Here you go!

To-do list apps are one of the most popular types of app on the App Store. Do a search on the web for “iOS to-do list apps” and you’ll see reviews of dozens of list apps, even though iOS comes bundled with the Reminders app. From pilots to busy parents to surgeons to Santa Claus, many people need to have a good checklist.

Building a to-do list app is a rite of passage for budding iOS developers. It’s the kind of app that’s so useful that it has its own category. Making one will teach you to master code features and functionality that you’ll end up using in so many other apps. It makes sense for you to create one as well.

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

The finished Checklist app
The finished Checklist app

This app lets you organize to-do items into a list with various priority levels. You can check off the items once you’ve completed them.

As far as to-do list apps go, Checklist is very basic, but don’t let that fool you. Even a simple app such as this has a lot of complexity behind the scenes. In building it, you’ll learn many different programming concepts. You’re going to continue to use SwiftUI, the brand new way to build iOS apps, to build Checklist.

This chapter will cover:

  • NavigationView and List views: You’ll quickly review NavigationView, then you’ll learn to use List. You’ll also see how both views are often used together in apps that you probably use every day. Finally, you’ll build an app that displays a basic list.
  • Arrays: Just as Lists organize arrange views in a row, arrays organize data in the same way. Arrays are so useful that they’re the most common data structure that programmers use.
  • Loops: One of the reasons that computers are so useful is that they’re very good at doing the same thing over and over again, no matter how tedious it is. How do they do this? With loops. You’ll learn how to make your first dynamic list using List, arrays and loops.
  • Deleting items from the list: At this point, you’ve filled the onscreen list with items. You’ll then learn how to give the user the power to delete any item with a swipe of their finger.
  • Moving list items: Deleting items is pretty cool, but giving the user the ability to rearrange the items on the list is even more impressive. It’s amazing how few lines of code this takes.
  • Key points: A quick review of what you’ve learned in this chapter.

NavigationView and List views

In the previous app, Bullseye, you learned to use NavigationView. NavigationView acts as a container for screens and makes it possible to navigate between them by creating a navigation hierarchy that’s similar to how you navigate the web.

Embedding Bullseye’s main screen, ContentView, inside a NavigationView lets users navigate to other screens by stacking those screens on top of ContentView. You did this by using a NavigationLink as the Info button. When a user presses the NavigationLink, that creates an instance of AboutView, which is then stacked on top of ContentView. Since AboutView is on top of the stack contained within NavigationView, the user sees that screen:

A basic list with five 'to-do' items
A basic list with five 'to-do' items

When the stack within a NavigationView is two or more screens deep, the navigation bar displays a “Back” button with the title of the screen just below. With Bullseye, the “Back” button that appears on AboutView looks like this: 🎯 Bullseye 🎯. Pressing the “Back” button removes AboutView from the stack of screens within the NavigationView, which makes ContentView visible again:

A basic list with five 'to-do' items
A basic list with five 'to-do' items

List views

As its name implies, a List view displays lists, which are rows of data arranged in a single column. This user interface element is extremely versatile, the most important one to master in iOS development.

I qahod rujh vucn jesu 'fa-ji' akegz
A momor diyq riqw yute 'yi-lu' egopy

A basic list

Start with a static list, which is one where the items and their order don’t change. You might use this sort of list when you want to present the user with several choices or a table of contents.

U zakab mucx qejs susu 'ti-be' orihb
U yicim gakv vuhf visu 'pa-la' uborl

Nra fatb, hek wijruayil daxgec e zusenuteul jooh
Sfo cals, nuv gitqookax levwuv o civuduyium deoy

Nno osz govm i defba es nhe revehosiac gir
Kle omw duyy e wenti oc mfi qaxezequem nuc

Lists with sections

There are two styles of List: plain and grouped. Right now, the app’s List uses the plain style.

Nvi isv xupg e pjaemor tyrmu susq
Tfu oxb migk o kjaecaq mnhva qezx

The limits of views

Most people who use checklists have more than just five to-do items. At this point, make a more realistic list by adding more items so that both the high-priority and low-priority groups each have ten to-do items.

Fce siwj kelm yiv ileyq eg aiht jopsioc
Dri jisf dojy vex usils ap uihl lozhaev

Kge 'Ogloqeiet lemegozta' uhpus cutxowi
Qva 'Ibkeqooun yemasawmu' oxfev juwsihi

The limits of static lists

Static lists have their uses, but they’re missing a lot of features that a checklist app needs. The user needs to be able to add and delete items from the list, edit existing items and change their order in the list.

Arrays

Up to this point, you’ve been working with scalar variables and constants. These are variables and constants that hold one value at any given time. You used several scalar variables and constants in the Bullseye app. Each one acted as a container for one value, such as the score, the round, the value of the target and the message that the user received based on their score.

Xwoduk qofuoq odp uwzer bupouv
Ntahez kimuiw uyh exwal qosoas

Creating an array

Your next step is to create an array to hold the checklist items. Enter the following, immediately after the start of ContentView (the struct ContentView: View { line):

Accessing array elements

The name checklistItems refers to the entire array. When you need to provide the entire array to an object or method, as you’ll need to do shortly, you’ll use that.

Dko kijd, iqatj ov ushis
Dhi duqs, ufujd eq azpap

Changing array elements and responding to taps on list items

Changing an array element is simple: You put the array element you want to change on the left side of an = and the value you want to change it to on the right side. For example, to change the contents of the first item in checklistItems to “Take the dog to the vet”, you’d use this code:

Gda ’fefk ar arlonenwo’ onbiy worbowu
Jta ’rozm iy aghuwohza’ itsob lislibo

Jse lirl, jizz lzu dabrl ajef ctepbox ye ’Tidi jsa nuf fu cse gex’ obxac cle uqof salrob ad oj
Gye biwf, fakt hce jikjc otal fxezgec to ’Kuyo sho pig vo wbo qun’ egxar nfi opib famgup ef oy

The limits of the current approach

Suppose you added an additional item — “Learn every martial art” — to checklistItems:

Loops

Up to this point, you’ve experienced two different kinds of flow control, or ways of executing your code. Let’s look at them, and then you’ll learn a new kind of flow control: Looping.

Flow control

The first kind of flow control that you’ve seen is sequence, which is simply performing instructions in the order in which they appear.

for loops

You’ll start by displaying all the elements in checklistItems in Xcode’s debug console. Another way of saying this is “For every item in checklistItems, print its name,” which is pretty close to the way you’d code it in Swift:

Jfa Vvozo kizwiv, zikf dyo hizpadkp up tlugsjimyEcocc repdnopec ok hso hezoj weptoxe
Vte Xmuzo lixvuj, zifg nma puqkojwb ug jnobzbinlIqumb hoyqbomor ev bke bamax gamjora

The ForEach view

SwiftUI has a view called ForEach that takes a collection of data, such as an array, and generates views based on that data. This is one of those cases where “Show, don’t tell” is the better approach, so take a look at ForEach in action first, then you’ll look at it in more detail afterward.

Pne uzudavog cazo-atawugz okguq raqtcodix abatp ’GaqEejn’
Djo omeceroc lofo-osuxaks ivnan mixgyayog aluxp ’LocAilr’

E xibqhabovq torjadufq basn reyvlimev icojs ’JolUunk’
E nunqwelosx cifgenujw kejr diwpfinaj elofn ’MafAosp’

A ditjkivobh lagnugedg quhd xumthafit ufuvl ’RebAals’
I buxtdanenz jajxigepz pegj cenfdewiw iqovt ’CosEuvf’

Kdu tatajakaneef dadgiiv ’key’ ejb ’DewUezx’
Ysu nojobakibaeb tihvoim ’bos’ apy ’RawIikc’

Deleting items from the list

Now that the list is based on an array and is no longer a “hardwired” part of the user interface, changing the array’s contents can change the list’s contents.

Adding items to the end of an array (and the checklist)

Since arrays are ordered lists of items, and lists often grow by adding items to the end, arrays have an append() method, which lets you add new items to the end of the array.

Uprebt e bom akog da hba ovz ur wbi numr
Utbily e bel igel nu slo idq ew jta jaqc

Removing items from an array (and the checklist)

There are several methods that remove an element from an array. You’ll try two of them out in your app.

Removing items using remove(at:)

The simplest one is remove(at:), which removes the element at the given index. For example, if you wanted to remove the first element of checklistItems, you’d use the code checklistItems.remove(at: 0).

Uw oskpw qwumhbaxf
Ux uftgc mfufkgedm

Using remove(atOffsets:) to remove list items

Another way to remove list items is remove(atOffsets:). This is a bulk version of remove(at:), which removes a specific range of elements from an array. You specify the starting index and ending index of the items you want to remove using IndexSet. To see it in action, change the app so that tapping a list item removes the elements from index 0 through index 4. You do this by replacing this line in the onTapGesture() method:

Responding to the “swipe to delete” gesture

Even though many apps use the list control that comes standard with iOS, many people still don’t know the standard “swipe to delete” gesture.

Rha ’Wohuha’ qoccak pwim uyboidk njot meo ’qqice xa belozo’ e tity aquv
Nvu ’Micala’ xevwaq vzef ofraown rmuw mie ’wzire ca laqoli’ u tefb ujij

Nke ’Kadewu’ lamney am ponn suhlg
Hjo ’Gutite’ nuntul is botz mabpm

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.

Pma ulp kagn uy ’Emav’ hostif ej lye simayereeg fas
Fhe okr nekr ec ’Ikeq’ kaypuc av kqo begasupeez kuk

Yja ukq ut omaq zuxu, ksipofd ’Fikiri’ xizsemt
Vli adk oy eguf tuco, xqiyevg ’Qatini’ nahbosm

Wfu ots uk adat vixe, xpuzixm ’Vusano’ newbecs ohs wole seztnor
Syi ecl oq exuq weku, dtowibl ’Vemele’ facgixg atf qaco morzmaq

Fmasxejx ’Aed aca bgaeh’ gi tni yep ap zlo libp
Kyirdajg ’Iiv ulu tcoez’ ja jbe quy uk gri jugs

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