有没有一种方法来创建一系列项目来填充都具有导航链接的列表视图?我试图使下面的代码起作用,以便使填充列表视图的每个项目都可以链接到另一个对列表中的项目进行描述的视图。预先感谢您提供的所有帮助!
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()
}
}
答案 0 :(得分: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()
}
}