Home > Back-end >  SwiftUI dynamically add items to List
SwiftUI dynamically add items to List

Time:06-20

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()
        }
    }

CodePudding user response:

addRestaurant() needs to be contained within the ContentView struct. Otherwise, you do not have access to the restaurants variable that was created in ContentView. This is referred to as the scope of the variable. You can only access it within its own scope, so, if defined in a struct, it is only available within the struct. Google Swift/SwiftUI scope and you will find a great deal of information. Therefore, your code would change to:

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 {
            addRestaurant()
        }
    }
    func addRestaurant(){
        restaurants.append(Restaurant(name: "Joe's", stars: "2"))
    }
}
  • Related