Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SwiftUI Cookbook

You're reading from   SwiftUI Cookbook A guide to solving the most common problems and learning best practices while building SwiftUI apps

Arrow left icon
Product type Paperback
Published in Nov 2021
Publisher Packt
ISBN-13 9781803234458
Length 616 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Giordano Scalzo Giordano Scalzo
Author Profile Icon Giordano Scalzo
Giordano Scalzo
Edgar Nzokwe Edgar Nzokwe
Author Profile Icon Edgar Nzokwe
Edgar Nzokwe
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Chapter 1: Using the Basic SwiftUI Views and Controls 2. Chapter 2: Going Beyond the Single Component with Lists and Scroll Views FREE CHAPTER 3. Chapter 3: Exploring Advanced Components 4. Chapter 4: Viewing while Building with SwiftUI Preview 5. Chapter 5: Creating New Components and Grouping Views with Container Views 6. Chapter 6: Presenting Extra Information to the User 7. Chapter 7: Drawing with SwiftUI 8. Chapter 8: Animating with SwiftUI 9. Chapter 9: Driving SwiftUI with Data 10. Chapter 10: Driving SwiftUI with Combine 11. Chapter 11: SwiftUI Concurrency with async await 12. Chapter 12: Handling Authentication and Firebase with SwiftUI 13. Chapter 13: Handling Core Data in SwiftUI 14. Chapter 14: Creating Cross-Platform Apps with SwiftUI 15. Chapter 15: SwiftUI Tips and Tricks 16. Other Books You May Enjoy

Adding sections to a list

In this recipe, we will create an app that implements a static list with sections. The app will display a list of countries grouped by continent.

Getting ready

Let's start by creating a new SwiftUI app in Xcode named ListWithSections.

How to do it…

We will add a Section view to our List to separate groups of items by section titles. Proceed as follows:

  1. (Optional) Open the ContentView.swift file and replace the Text view with a NavigationView. Wrapping the List in a NavigationView allows us to add a title and navigation items to the view:
    NavigationView{
    }
  2. Add a list and section to NavigationView (or body view if you skipped optional Step 1). Also, add a listStyle and navigationBarTitle modifier:
    List {
        Section(header: Text("North America")){
            Text("USA")
            Text("Canada")
            Text("Mexico")
            Text("Panama")
            Text("Anguilla")
        }
    }
    .listStyle(.grouped)
    .navigationBarTitle("Continents and Countries",
      displayMode: .inline)
  3. Below the initial Section, add more sections representing countries in various continents:
                List {
                    …
                    Section(header: Text("Africa")){
                        Text("Nigeria")
                        Text("Ghana")
                        Text("Kenya")
                        Text("Senegal")
                    }
                    Section(header: Text("Europe")){
                        Text("Spain")
                        Text("France")
                        Text("Sweden")
                        Text("Finland")
                        Text("UK")
                    }
                }

    Your canvas preview should resemble the following:

Figure 2.9 – ListWithSections preview

Figure 2.9 – ListWithSections preview

Looking at the preview, you can see the continents where each country is located by reading the section titles.

How it works…

SwiftUI's Section views are used to separate items into groups. In this recipe, we used Section views to visually group countries by their continents. A Section view can be used with a header, as shown in this recipe, or without a header, as follows:

                Section {
                    Text("Spain")
                    Text("France")
                    Text("Sweden")
                    Text("Finland")
                    Text("UK")
                }

You can change section styles by using the listStyle() modifier with the .grouped style.

You have been reading a chapter from
SwiftUI Cookbook - Second Edition
Published in: Nov 2021
Publisher: Packt
ISBN-13: 9781803234458
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image