Creating Array of Navigation Links for a List View for More than 10 Items in SwiftUI

834 views Asked by At

Is there a way to create an array of items to populate a list view that all have navigation links? I am trying to make the below code work so that each item that populates the list view can then link to a another view that has descriptions of the item in the list. Thanks in advance for any and all help!

    import SwiftUI
    
    struct Restaurant: Identifiable {
    var id = UUID()
    var name: String
    //var destination: 
}

struct RestaurantRow: View {
    var restaurant: Restaurant

    var body: some View {
        Text("Come and eat at \(restaurant.name)")
    }
}

struct Parts: View {
    
    var body: some View {
        
        let restaurants = [Restaurant(name: "1"), Restaurant(name: "2"), Restaurant(name: "3"), Restaurant(name: "4"), Restaurant(name: "5"), Restaurant(name: "6"), Restaurant(name: "7"), Restaurant(name: "8"), Restaurant(name: "9"), Restaurant(name: "10"), Restaurant(name: "11"), Restaurant(name: "12"), Restaurant(name: "1"), Restaurant(name: "2"), Restaurant(name: "3"), Restaurant(name: "4"), Restaurant(name: "5"), Restaurant(name: "6"), Restaurant(name: "7"), Restaurant(name: "8"), Restaurant(name: "9"), Restaurant(name: "10"), Restaurant(name: "11"), Restaurant(name: "12")]

        return List(restaurants) { restaurant in
            RestaurantRow(restaurant: restaurant)
        }
        .listStyle(GroupedListStyle())
        .environment(\.horizontalSizeClass, .regular)

    }
}

struct Parts_Previews: PreviewProvider {
    static var previews: some View {
        Parts()
    }
}
1

There are 1 answers

2
nicksarno On

Is this what you're looking for?

import SwiftUI
 
 struct Restaurant: Identifiable, Hashable {
    var id = UUID()
    var name: String
 }

struct RestaurantRow: View {
 var restaurant: Restaurant

 var body: some View {
     Text("Come and eat at \(restaurant.name)")
 }
}

struct Parts: View {
    
    let restaurants = [Restaurant(name: "1"), Restaurant(name: "2"), Restaurant(name: "3"), Restaurant(name: "4"), Restaurant(name: "5"), Restaurant(name: "6"), Restaurant(name: "7"), Restaurant(name: "8"), Restaurant(name: "9"), Restaurant(name: "10"), Restaurant(name: "11"), Restaurant(name: "12"), Restaurant(name: "1"), Restaurant(name: "2"), Restaurant(name: "3"), Restaurant(name: "4"), Restaurant(name: "5"), Restaurant(name: "6"), Restaurant(name: "7"), Restaurant(name: "8"), Restaurant(name: "9"), Restaurant(name: "10"), Restaurant(name: "11"), Restaurant(name: "12")]
 
 var body: some View {
    NavigationView {
        List {
            ForEach(restaurants, id: \.self) { restaurant in
                NavigationLink(
                    destination:
                        Text("NEXT VIEW GOES HERE")
                    ,
                    label: {
                        RestaurantRow(restaurant: restaurant)
                    })
            }
        }
        //.environment(\.horizontalSizeClass, .regular)
        //.listStyle(GroupedListStyle()) // Optional
        //.navigationTitle("Title goes here") // Optional
        //.navigationBarTitleDisplayMode(.inline) // Optional
        .navigationBarHidden(true) //Optional
    }

 }
}

struct Parts_Previews: PreviewProvider {
 static var previews: some View {
     Parts()
 }
}