3
votes

I'm trying to create a view with two nested List Views. That means the main view has rows and each row also has rows. Because of a reordering requirement (edit mode) and swipe to delete (inner rows only), I can't just use ForEach-loops with a scroll view. The problem is the following: the inner rows don't show when I tap "+add set" although debugging shows they are actually added. the reason might be that view of the outer row doesn't adapt its height. I know this because if I only use ForEach and no List, the rows do appear. but then I can't use swipe to delete. this is the code for the inner rows (as List view) :

       List {
                ForEach(self.exercise.sets) { set in
                    SetRow(set: set, exercise: self.exercise)
                }.onDelete { (offsets) in
                    self.exercise.sets.remove(atOffsets: offsets)
                }
        }

each outer row has VStack before this List (for the exercise name and the column titles) and a button below. see the screen shots Inner list without List, only ForEach loop

Althout object for the inner row was added, the row doesn't appear

1

1 Answers

1
votes

seems I found a workaround... The outer list looks like this in code:

 List() {

      Text("New workout").font(.title)

       ForEach(self.workoutModel.exercises) {exercise in
            ExerciseWorkoutView(exercise: exercise)
        }



  }

I simply added the following frame modifier to the ExerciseWorkoutView (which is a VStack with a List view in it:

.frame(height: self.minFrameHeight + self.setRowHeight * CGFloat(self.exercise.sets.count)).animation(.default)

minFrameHeight and setRowHeight are constants I set.

self.exercise is an observed object with a sets array as @Published instance variable. that's why the frame height adapts automatically...

if anyone knows a better solution, thanks for posting. I tried several variations of .fixedSize(...) already but it didn't work.