Chapter 2: Going Beyond the Single Component with Lists and Scroll Views
In this chapter, we'll learn how to display lists in SwiftUI. List
views are similar to UITableViews
in UIKit but are significantly simpler to use. No storyboards or prototype cells are required, and we do not need to know how many rows there are. SwiftUI's lists are designed to be modular so that you can build bigger things from smaller components.
We'll also look at lazy stacks and lazy grids, which are used to optimize the display of large amounts of data by loading only the subset of the content that is currently being displayed or is about to be displayed. Lastly, we'll take a look at how to present hierarchical data in an expanding list with sections that can be expanded or collapsed.
By the end of this chapter, you will understand how to display lists of static or dynamic items, add or remove rows from lists, edit lists, add sections to list views, and much more.
In this chapter, we will cover the following recipes:
- Using scroll views
- Creating a list of static items
- Using custom rows in a list
- Adding rows to a list
- Deleting rows from a list
- Editing a list
- Moving rows in a list
- Adding sections to a list
- Using
LazyHStack
andLazyVStack
(iOS 14+) - Using
LazyHGrid
andLazyVGrid
(iOS 14+) - Using
ScrollViewReader
(iOS 14+) - Using expanding lists (iOS 14+)
- Using
DisclosureGroup
to hide and show content (iOS 14+)