SwiftUI Expandable Lists (Part 2)

Michalis Karagiorgos
3 min readJul 15, 2024

--

Using isExpanded binding and DisclosureGroup

In the previous article here (Part 1) we managed to create a Hierarchical Expandable SwiftUI List with some UI customizations like:

  1. remove row separators
  2. remove the insets for the nested elements
  3. change the chevron image and animation

What if we need though to have every element initialy expanded for all items? Let’s try to also implement that requirement.

Step 1

We need to update our models with an extra expanded Boolean property

Let’s also create a mock array of 1000 events per league, 2 leagues per sport and 3 Sports (6000 elements total to check if our handling can lead to performance issues, something not so difficult when using SwiftUI Lists 😛)

Step 2

Let’s create a different DisclosureGroup per item type for types that have children (Sport, League) and also create a nice EventView

Step 4

Let’s wrap all inside a Contentview using a plain List

A nice trick when working with SwiftUI and Lists (applies also UIKit) is to use the view debugger to ensure that everything works as expected.

At the left side you can see that we have only the visible cells in memory and how the View Host actually creates the UI Elements. It seems like for our case the SwiftUI List just creates a simple UICollectionView

Next steps

What if we need to also support extra features here like count down clocks for events? Can SwiftUI also support this without any performance issue?

To be continued…

Thank you!

--

--