带有 ForEach 的 SwiftUI List 在一个 VStack 中工作,但在第二个 VStack 中不工作
SwiftUI List with ForEach works in one VStack but not in second one
我完全不知道为什么我试图在 SwiftUI
中创建的列表(使用 ForEach
)会以现在的方式运行。我有一个 ObservedObject
,我在检查器中验证了它具有我期望的数据。在第一个 VStack
中,我可以用 ForEach
创建列表,它输出得很好。在第二个 VStack
中,我没有得到任何数据输出。
struct WorkoutPreview: View {
@Environment(\.managedObjectContext) var managedObjectContext
@ObservedObject var workoutSessionExercises: WorkoutSessionExercises
var body: some View {
NavigationView {
VStack {
Text("Welcome to your workout! Below are the exercises and bands you've selected for each.")
.padding()
Text("We found \(workoutSessionExercises.workoutExercises.count) workouts.")
.padding()
// This List (with ForEach) works fine and produces output.
List {
ForEach(0..<workoutSessionExercises.workoutExercises.count) { index in
Text(self.workoutSessionExercises.workoutExercises[index].exerciseName)
}
}
}
// The exact same List (with ForEach) in this VStack produces no results.
VStack {
List {
ForEach(0..<workoutSessionExercises.workoutExercises.count) { index in
Text(self.workoutSessionExercises.workoutExercises[index].exerciseName)
}
}
}
Spacer()
}
}
}
问题是您有 2 个 VStack,它们在视图中不是一个在另一个下面,因此您看不到第二个列表。
要解决您的问题,请将您的 VStack 包装在另一个 VStack 中,例如:
var body: some View {
NavigationView {
VStack { // <------
VStack {
Text("Welcome to your workout! Below are the exercises and bands you've selected for each.").padding()
Text("We found \(workoutSessionExercises.workoutExercises.count) workouts.").padding()
// This List (with ForEach) works fine and produces output.
List {
ForEach(0..<workoutSessionExercises.workoutExercises.count) { index in
Text(self.workoutSessionExercises.workoutExercises[index].exerciseName)
}
}
}
// The exact same List (with ForEach) in this VStack produces no results.
VStack {
List {
ForEach(0..<workoutSessionExercises.workoutExercises.count) { index in
Text(self.workoutSessionExercises.workoutExercises[index].exerciseName)
}
}
}
Spacer()
} // <------
}
}
我完全不知道为什么我试图在 SwiftUI
中创建的列表(使用 ForEach
)会以现在的方式运行。我有一个 ObservedObject
,我在检查器中验证了它具有我期望的数据。在第一个 VStack
中,我可以用 ForEach
创建列表,它输出得很好。在第二个 VStack
中,我没有得到任何数据输出。
struct WorkoutPreview: View {
@Environment(\.managedObjectContext) var managedObjectContext
@ObservedObject var workoutSessionExercises: WorkoutSessionExercises
var body: some View {
NavigationView {
VStack {
Text("Welcome to your workout! Below are the exercises and bands you've selected for each.")
.padding()
Text("We found \(workoutSessionExercises.workoutExercises.count) workouts.")
.padding()
// This List (with ForEach) works fine and produces output.
List {
ForEach(0..<workoutSessionExercises.workoutExercises.count) { index in
Text(self.workoutSessionExercises.workoutExercises[index].exerciseName)
}
}
}
// The exact same List (with ForEach) in this VStack produces no results.
VStack {
List {
ForEach(0..<workoutSessionExercises.workoutExercises.count) { index in
Text(self.workoutSessionExercises.workoutExercises[index].exerciseName)
}
}
}
Spacer()
}
}
}
问题是您有 2 个 VStack,它们在视图中不是一个在另一个下面,因此您看不到第二个列表。 要解决您的问题,请将您的 VStack 包装在另一个 VStack 中,例如:
var body: some View {
NavigationView {
VStack { // <------
VStack {
Text("Welcome to your workout! Below are the exercises and bands you've selected for each.").padding()
Text("We found \(workoutSessionExercises.workoutExercises.count) workouts.").padding()
// This List (with ForEach) works fine and produces output.
List {
ForEach(0..<workoutSessionExercises.workoutExercises.count) { index in
Text(self.workoutSessionExercises.workoutExercises[index].exerciseName)
}
}
}
// The exact same List (with ForEach) in this VStack produces no results.
VStack {
List {
ForEach(0..<workoutSessionExercises.workoutExercises.count) { index in
Text(self.workoutSessionExercises.workoutExercises[index].exerciseName)
}
}
}
Spacer()
} // <------
}
}