Today We are going to learn about how to add List in swiftui of Static Items. Initally We will start with the Static items .
First we need to define the what are the elements in the list row how it will look like. Once you have the row , you can create a List View that will create the number of rows required for the number of items to be listed.
struct IceCream: View {
let name: String
var body: some View {
Text("IceCream Parlour: (name)")
}
}
struct ContentView: View {
var body: some View {
List {
IceCream(name: "Vanila")
IceCream(name: "Mango")
IceCream(name: "Pineapple")
IceCream(name: "Orange")
}
}
}
In This Above Example the code defines a IceCream View that will show a name of String which we will show in List with the 4 fixed data as IceCream(name: “Vanila”)
Now We will start with the List of Dynamic items. Basically in order to handle the Dynamic Item in the list View we need to define the identifiable protocol and we need to add the id value so that SwiftUI can use to see which item it is.
struct Parlour: Identifiable {
let id = UUID()
let IceCreamname: String
}
We need to add Protocol to Parlour as Identifiable in swiftUI. use the id and the Icecreamname in the model .