How to create a list of dynamic items
How to create a list of dynamic items êŽë š
Updated for Xcode 15
In order to handle dynamic items, you must first tell SwiftUI how it can identify which item is which. This is can either be done by specifying the identifying property by hand, or by using the Identifiable
protocol. This protocol has only one requirement, which is that the type must have some sort of id
value SwiftUI can use to see which item is which.
To demonstrate this, we can create three things then put them together:
- A
Restaurant
struct that says restaurants have an ID and name, with the ID being a random identifier just so that SwiftUI knows which is which. - A small view that determines what each row in our list looks like. In our case we're going to define a
RestaurantRow
view that stores one restaurant and prints its name in a text view. - A list view that shows several restaurants. This means creating some example data, putting it into an array, then passing that into a list to be rendered.
Here's all that in code:
// A struct to store exactly one restaurant's data.
struct Restaurant: Identifiable {
let id = UUID()
let name: String
}
// A view that shows the data for one Restaurant.
struct RestaurantRow: View {
var restaurant: Restaurant
var body: some View {
Text("Come and eat at \(restaurant.name)")
}
}
// Create three restaurants, then show them in a list.
struct ContentView: View {
let restaurants = [
Restaurant(name: "Joe's Original"),
Restaurant(name: "The Real Joe's Original"),
Restaurant(name: "Original Joe's")
]
var body: some View {
List(restaurants) { restaurant in
RestaurantRow(restaurant: restaurant)
}
}
}
Most of that is just creating data â the last part is where the real action is: using List(restaurants)
creates a list from the restaurants
array, executing the following closure once for every item in the array. Each time the closure goes around the restaurant
input will be filled with one item from the array, so we use that to create a RestaurantRow
.
In fact, in trivial cases like this one we can make the code even shorter: List(restaurants, rowContent: RestaurantRow.init)
does exactly the same thing.