【问题标题】:SwiftUI List with ForEach works in one VStack but not in second one带有 ForEach 的 SwiftUI 列表在一个 VStack 中有效,但在第二个 VStack 中无效
【发布时间】:2020-06-17 20:11:20
【问题描述】:

我完全不知道为什么我试图在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()
    }
  }
}

【问题讨论】:

    标签: swiftui swiftui-list


    【解决方案1】:

    问题是您有 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()
            }         //  <------
        }
    }
    

    【讨论】:

    • 感谢您,它将它们都包装在 VStack 中。你能帮我理解为什么同一级别的两个VStack 元素不能访问同一个对象吗?我很困惑他们如何生活在同一个层次结构级别,但只有第一个可以看到它。
    • NavigationView 应该环绕整个容器,例如; List、ScrollView、VStack、HStack、Form 等...如果没有,您会遇到遇到的问题。为什么我真的不知道。苹果文档没有解释。要在垂直方向上一个接一个地获得两个视图,它们需要位于 VStack 中。您的列表访问锻炼锻炼对象没有任何问题。即使您使用 Text("...") 或任何其他视图,问题仍然相同。这些视图在哪里显示,我不知道,我的猜测是,只有第一个被显示,其他的被忽略。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多