0

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
}

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.