Iterating views
In this section, I will describe how you can display multiple views, as you previously would have done in UIKit using UITableViews
and UICollectionViews
.
In UIKit, you would have, for the purpose of visualizing tables and grids, created derived classes inheriting from UITableViewControllers
and UICollectionViewControllers
. To control their appearance and behavior, you would have used properties and delegate methods.
In SwiftUI, you have several ways to achieve this result. The first is iterating, that is, repeating views with a loop. You can achieve this with the specialized view called ForEach
. ForEach
does not have the limitation of 10 maximum views that affect views entered manually.
For example, the following fragment of code will create a form containing 90 subviews:
Form { ForEach (0..<90) { number in Text("Hello...