Better Programming

Advice for programmers.

5 SwiftUI List Tricks for iOS 14+

Mohd Hafiz
Better Programming
Published in
4 min readAug 5, 2021

--

Drawing of layouts on phone screen
Image by the author

List is a great SwiftUI component to create a list view that contains multiple row views. It’s quite similar to UITableView, but it has a totally different implementation (no more delegate and data source methods).

List in SwiftUI uses declarative code, which means that it will observe the data changes and efficiently update the rendered result solely based on what has changed.

In this article, we will discuss five basic features provided in the List:

  1. Multiple sections
  2. Grouped and inset
  3. Deleting row item (single and bulk mode)
  4. Expandable List
  5. Navigation

Know the Basics

Before we go through all those five features, let’s take a look at the basic syntax and implementation of the List.

First, create a new SwiftUI project to play around with examples.

Create new SwiftUI project on Xcode

Static List

A list where all the items are statically defined in the List closure. Add the below code to the project:

Example of static List
Code and demo of basic implementation
Preview output of static List

Dynamic List With Custom Row View

First, we need to create the CustomRowView struct and define simple content. Then, make it dynamic by passing an argument in List(). See the code below to show the list from bookTitles array:

Code and demo of basic implementation
Preview output for dynamic List example

Using ForEach

Take note that dynamic content can also be done using ForEach closure. Here’s an example:

Dynamic list with ForEach syntax

Cool, I guess we are fine with the how the basic List works. Now, let’s proceed with the five List features.

1. Multiple Sections

In the below code, we will create two sections (foods and drinks) that contain dynamic items. All we need is to do the following:

Code and demo of trick 1
Preview output for a List with Section example

2. Grouped and Inset

For grouping and inset, SwiftUI provides few options, including InsetGroupedListStyle(), GroupedListStyle(), InsetListStyle(). I personally love the InsetGroupedList Style. Its default padding is already good and clean. Here’s the code:

Code and demo of trick 2
Preview output for a List with Section and GroupInset

3. Deleting Row Item (Single and Bulk Mode)

For iOS13 and above, onDelete() is supported by default, which allows users to swipe and tap on the default “Delete” button. However, in iOS15, an update is added to List to call the new swipeActions() function as discussed in detail in this article.

Great, let’s add the onDelete() function for now. Take note that we will add the property wrapper @State to bookTitles to make it mutable within the View struct. Also, the List will automatically refresh once we updated the variable such as adding or removing items. Here’s the code to do that:

Demo of trick 3
Swipe to delete example

Bulk Delete

In addition, SwiftUI provides the EditButton feature which enables edit mode within the specified scope. So, we will update a bit with navigation and adding EditButton to the right top to enable the delete option for all in the list. See the below code and output:

Demo of trick 3 with bulk delete
Bulk delete using EditButton example

4. Expandable List

List has a feature where it can pass the sub-items or children with the same type. Let’s take a look at the below example:

First, we will create a basic struct to be used with the mock data.

Then, add a List and pass the subItem field as the children argument. As we can see, the most important part is the data, which determines whether or not the List has its children.

Demo of trick 4
Expandable List example output

5. Navigation

In this section, let’s revisit our basic dynamic list and make it clickable using NavigationLink closure.

First, we will create a simple DetailView, as shown below:

Then, update the BasicDynamicView with the following code:

Demo of trick 5
Navigation of List example

Project Completed

Congratulations! Now, we have completed all the basic five features. The complete source code can be downloaded from my GitHub repository. Hopefully, it will help you learn SwiftUI and improve your iOS development skills.

Thanks for reading. Happy coding!

--

--

Mohd Hafiz
Mohd Hafiz

Written by Mohd Hafiz

iOS Developer focusing on Swift — “Read, learn & practice!”.

Responses (1)