I'm new to SwiftUI. I'm just wondering how I could insert new items to my List by using the function addRestaurant. restaurants.append() ... is not working in addRestaurant. The list should be refreshing the items as a user hits the button.
Here is the code so far:
import SwiftUI
struct Restaurant: Identifiable {
let id = UUID()
let name: String
let stars: String
}
struct RestaurantRow: View {
var restaurant: Restaurant
var body: some View {
HStack
{
Image(systemName: "photo")
Text("\(restaurant.name)")
Text("Stars \(restaurant.stars)").foregroundColor(Color.yellow)
}
}
}
struct ContentView: View {
@State var restaurants = [
Restaurant(name: "Joe's Original", stars: "5"),
Restaurant(name: "The Real Joe's Original", stars: "4"),
Restaurant(name: "Original Joe's", stars: "3")
]
var body: some View {
NavigationView{
List(restaurants)
{ restaurant in
RestaurantRow(restaurant: restaurant)
}
.navigationBarItems(
trailing: Button(action: addRestaurant, label: { Text("Add")}))
.navigationTitle("Table View Playground")
}.refreshable {
restaurants.append(Restaurant(name: "Joe's", stars: "2"))
}
}
}
func addRestaurant(){
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}