I try to make expanding sections from dictionary of Array [String: [Int]] in SwiftUI. This Code worked but without expanding. how can i do it?
var body: some View {
let dict : [String: [Int]] = ["key1": [1,2,3,4], "key2": [6,7,8,9]]
Section {
List(dict.keys.sorted(), id: \.self) { key in
Section(header: Text(key)) {
ForEach(dict[key]!, id: \.self) { x in
Text("\(x)")
}
}
} // List
} // Section
}
I found DisclosureGroup
" in SwiftUI.
You can use the DisclosureGroup modifier to create an expandable list view in SwiftUI.
And this is how the code should be:
var body: some View {
let dict : [String: [Int]] = ["key1": [1,2,3,4], "key2": [6,7,8,9]]
Section {
List(dict.keys.sorted(), id: \.self) { key in
DisclosureGroup(key) {
ForEach(dict[key]!, id: \.self) { x in
Text("\(x)")
}
}
} // List
} // Section
}