SwiftUI 中的免费可扩展列表有哪些要求?

What are the requirements for a free expandable List in SwiftUI?

在我的代码中的某个地方,我有这个非常标准的列表,其中包含以下部分:

var body: some View {
    List {
        ForEach(userData.groupedBookings) { group in
            Section(header: Text(group.key)) {
                ForEach(group.items) { booking in
                    LessonRow(booking: booking)
                }
            }
        }
    }
}

不知何故,这段代码的部分是 expandable/collapsable,这让我很高兴,但我不知道为什么。 我特别沮丧,因为我想用类似的代码在别处重现这种行为,但没有得到展开/折叠。

自动获取这个有什么要求?

它是由侧边栏列表样式激活的(在某些情况下被认为是默认样式),您可以显式使用它

List {
    ForEach(userData.groupedBookings) { group in
        Section(header: Text(group.key)) {
            ForEach(group.items) { booking in
                LessonRow(booking: booking)
            }
        }
    }
}
.listStyle(SidebarListStyle())

作为替代,您可以使用 DisclosureGroup 显式地对部分进行披露行为,例如