How to get collapsable sections as shown in 'Stacks, Grids, and Outlines in SwiftUI'?

I tried to replicate the code as shown in Stacks, Grids, and Outlines in SwiftUI (12:35).

They make it feel like it's a very easy thing to do. Just insert a Section and as you could see in the video it will immediately become a collapsable view. Add a header to the section and it will get the bolder text style. I wasn't able to replicate this.

xCode Version 12.0 beta (12A6159)

Below you can find the code I used.

The expected result would be nicely styled, collapsable section headers.

The actual result is a non-collapsable section with a weird underline under the name and no arrow.

Code Block struct ContentView: View {
  let model: Model = Model()
   
  @State private var selection: Set<String> = []
  var body: some View {
    NavigationView {
      List(selection: $selection) {
        ForEach(model.canvases) { canvas in
          Section(header: Text(canvas.name)) {
            OutlineGroup(canvas.graphics, children: \.children) { graphic in
              Label(graphic.name, systemImage: graphic.icon)
            }
          }
        }
      }
      .listStyle(SidebarListStyle())
      .navigationTitle("Graphics")
    }
  }
}
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}


It uses this simple model to hold the canvases and graphics:
Code Block
struct Model: Identifiable {
  let id: String = UUID().uuidString
  let canvases: [Canvas] = [
    Canvas(name: "Main", graphics: [
      Graphic(name: "Rectangle", icon: "rectangle.fill"),
      Graphic(name: "Oval", icon: "circle.fill"),
      Graphic(name: "Group", icon: "rectangle.3.offgrid", children: [
        Graphic(name: "Rectangle", icon: "rectangle.fill"),
        Graphic(name: "Rectangle", icon: "rectangle.fill"),
        Graphic(name: "Rectangle", icon: "rectangle.fill")
      ]),
      Graphic(name: "Group", icon: "rectangle.3.offgrid", children: [
        Graphic(name: "Rectangle", icon: "rectangle.fill"),
        Graphic(name: "Rectangle", icon: "rectangle.fill"),
        Graphic(name: "Rectangle", icon: "rectangle.fill")
      ])
    ]),
    Canvas(name: "Highlights", graphics: [
      Graphic(name: "Triangle", icon: "triangle.fill"),
      Graphic(name: "Triangle", icon: "triangle.fill")
    ]),
    Canvas(name: "Version 2", graphics: [
      Graphic(name: "Rectangle", icon: "triangle.fill"),
      Graphic(name: "Oval", icon: "circle.fill"),
      Graphic(name: "Triangle", icon: "triangle.fill")
    ]),
  ]
}
struct Canvas: Identifiable {
  var id: String = UUID().uuidString
  var name: String
  let graphics: [Graphic]
}
struct Graphic: Identifiable {
  var id: String = UUID().uuidString
  var name: String
  var icon: String
  var children: [Graphic]?
}


Accepted Reply

Apple Feedback
The client code is recreating the Model every time the view is rendered. The model generates the new ID for every item every time. Without consistent IDs, the system cannot maintain a consistent UI.

The client code should be using @StateObject on the model property in ContentView.

Replies

To get a more extensive description with images and code I also posted this question on StackOverflow: How to build a collapsible sidebar as shown in the Files app?

It seems like they solved the issue in iOS 14 beta 2. Not in Simulator but on the hardware device itself.
Altho the behavior is still very buggy.
1) When I press on a section header it get's a highlighted effect, which isn't there in the sidebar of the Files app.
2) When I collaps and expand the items, for instance the 'Version 2' section, the style of the first item get's styled two with bold and increased font, but when I collaps and expand again the last item get's styled and this swaps constantly.

Apple Feedback
The client code is recreating the Model every time the view is rendered. The model generates the new ID for every item every time. Without consistent IDs, the system cannot maintain a consistent UI.

The client code should be using @StateObject on the model property in ContentView.