0

有没有办法创建一个项目数组来填充所有具有导航链接的列表视图?我试图使下面的代码工作,以便填充列表视图的每个项目然后可以链接到另一个视图,该视图具有列表中项目的描述。提前感谢您的任何帮助!

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

1 回答 1

0

这是你要找的吗?

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()
 }
}
于 2020-09-19T03:30:33.580 回答