how to write a list in swiftui

Generated on 7/30/2024

1 search

Tailscan

This post is brought to you by Tailscan, the best devtool for Tailwind CSS.

To write a list in SwiftUI, you can use the List view, which is a powerful and flexible way to display a collection of data. Here are some key points and examples from the WWDC sessions:

  1. Basic List Initialization:

    • You can initialize a List with a collection of data. This is a convenient way to create a ForEach view, which generates views for each element in the collection and provides them to its container.
    • Example:
      struct Pet: Identifiable {
          let id = UUID()
          let name: String
      }
      
      let pets = [Pet(name: "Whiskers"), Pet(name: "Rufus"), Pet(name: "Bubbles")]
      
      struct PetListView: View {
          var body: some View {
              List(pets) { pet in
                  Text(pet.name)
              }
          }
      }
      
  2. Customizing Lists:

    • Lists can be customized through the use of view modifiers. For example, you can add swipe actions to each row.
    • Example:
      struct PetListView: View {
          var body: some View {
              List(pets) { pet in
                  Text(pet.name)
                      .swipeActions {
                          Button("Delete") {
                              // Handle delete action
                          }
                          .tint(.red)
                      }
              }
          }
      }
      
  3. Using ForEach within a List:

    • You can also create a list by manually writing out a set of views or by using a ForEach view.
    • Example:
      struct PetListView: View {
          var body: some View {
              List {
                  ForEach(pets) { pet in
                      Text(pet.name)
                  }
              }
          }
      }
      
  4. Searchable Lists:

    • You can make a list searchable by adding the searchable modifier.
    • Example:
      struct PetListView: View {
          @State private var searchTerm = ""
      
          var body: some View {
              List {
                  ForEach(pets.filter { searchTerm.isEmpty || $0.name.contains(searchTerm) }) { pet in
                      Text(pet.name)
                  }
              }
              .searchable(text: $searchTerm)
          }
      }
      

For more detailed information, you can refer to the following sessions from WWDC:

These sessions cover the basics of creating and customizing lists in SwiftUI, as well as more advanced topics like composition and state management.

What’s new in SwiftUI

What’s new in SwiftUI

Learn how you can use SwiftUI to build great apps for any Apple platform. Explore a fresh new look and feel for tabs and documents on iPadOS. Improve your window management with new windowing APIs, and gain more control over immersive spaces and volumes in your visionOS apps. We’ll also take you through other exciting refinements that help you make expressive charts, customize and layout text, and so much more.

Get started with Dynamic Type

Get started with Dynamic Type

Dynamic Type lets people choose their preferred text size across the system and all of their apps. To help you get started supporting Dynamic Type, we’ll cover the fundamentals: How it works, how to find issues with scaling text in your app, and how to take practical steps using SwiftUI and UIKit to create a great Dynamic Type experience. We’ll also show how you can best use the Large Content Viewer to make navigation controls accessible to everyone.

Migrate your TVML app to SwiftUI

Migrate your TVML app to SwiftUI

SwiftUI helps you build great apps on all Apple platforms and is the preferred toolkit for bringing your content into the living room with tvOS 18. Learn how to use SwiftUI to create familiar layouts and controls from TVMLKit, and get tips and best practices.

SwiftUI essentials

SwiftUI essentials

Join us on a tour of SwiftUI, Apple’s declarative user interface framework. Learn essential concepts for building apps in SwiftUI, like views, state variables, and layout. Discover the breadth of APIs for building fully featured experiences and crafting unique custom components. Whether you’re brand new to SwiftUI or an experienced developer, you’ll learn how to take advantage of what SwiftUI has to offer when building great apps.

Demystify SwiftUI containers

Demystify SwiftUI containers

Learn about the capabilities of SwiftUI container views and build a mental model for how subviews are managed by their containers. Leverage new APIs to build your own custom containers, create modifiers to customize container content, and give your containers that extra polish that helps your apps stand out.

What’s new in AppKit

What’s new in AppKit

Discover the latest advances in Mac app development. Get an overview of the new features in macOS Sequoia, and how to adopt them in your app. Explore new ways to integrate your existing code with SwiftUI. Learn about the improvements made to numerous AppKit controls, like toolbars, menus, text input, and more.