List
A List displays any number of views in a scrolling vertical column, optionally providing the ability to select one or more members.
// ⭐️ list consisting of a single type
List(1..<5) { Text("Item \($0)") }
List(names, id: \.self) { Text("Item \($0)") }
// ⭐️ static subviews
List {
Text("1")
Text("2")
Text("3")
}
// ⭐️ static & dynamic subviews
List {
// ⭐️ static
Toggle(isOn: $showFavoritesOnly) {
Text("Favorites Only")
}
// ⭐️ dynamic
ForEach(filteredLandmarks) { landmark in
NavigationLink {
LandmarkDetail(landmark: landmark)
} label: {
LandmarkRow(landmark: landmark)
}
}
}.navigationTitle("Landmarks")
seq.grouped(by:) - 將 Array 變成 Dictionary
can add Section header/footer.
To combine static and dynamic views in a List, or to combine two or more different groups of dynamic views, use the ForEach type instead of passing your collection of data to List.
List vs. ForEach
ForEach is a view that lets you pass a collection of data to its initializer and then creates multiple "subviews" from the closure you provide.
List is a view that can compose multiple views together, but not necessarily views of the same type. You can simply add multiple views without any loop.
As a convenience, the List initializer allows you to use it just like the ForEach view in case you want to have a list consisting of a single type only.
List
✅
✅
ForEach
❌
❌
SwiftUI ⟩
Collection Containers ⟩ List (struct)
listRowInsets(_:) - applies an inset to the rows in a list.
Foundation ⟩ Numbers, Data, and Basic Values ⟩ UUID (struct)
Last updated
Was this helpful?