How we can get access of DisclosureGroup of List in SwiftUI?

1.3k Views Asked by At

I am using this code for showing my parent and children data on a List in SwiftUI 2.0 whit macOs 10.15.7

And as default my parent are in Collapse status! I like to force some of them became expanded with my Button Action, I have that Buttons. Does anyone know how to solve this problem?

This is my code:

import SwiftUI

struct staticData: Identifiable
{
    let id                  = UUID()
    var NameOfItem          : String
    var dynamicData         : [staticData]?
}

let Child1 = staticData(NameOfItem: "Child1")
let Child2 = staticData(NameOfItem: "Child2")
let Child3 = staticData(NameOfItem: "Child3")

let parent1 = staticData(NameOfItem: "parent1", dynamicData: [Child1, Child2, Child3])
let parent2 = staticData(NameOfItem: "parent2", dynamicData: [Child1, Child2])
let parent3 = staticData(NameOfItem: "parent3")

struct ContentView: View
{
    
    
    @State var items: [staticData] = [parent1, parent2, parent3]

    var body: some View
    {
        VStack
        {
            HStack
            {
                
                Spacer()
                
                Button("Expand parent1")
                {
                    
                }
                
                Spacer()
                
                Button("Collapse parent1")
                {
                    
                }
                
                Spacer()
            }
            
            List(items, children: \.dynamicData) { row in
                
                Text(row.NameOfItem)
                    .onTapGesture
                    {
                        print(row.id)
                        print(row.NameOfItem)
                        print(row.dynamicData?.count ?? 0)
                    }
            }
        }
    }
}
1

There are 1 best solutions below

3
On BEST ANSWER

Here is a demo of possible approach. (Of course at your side it is better to separate on smaller sub-views, like HeaderView, ExpandableRowView, FlatRowView, etc)

struct staticData: Identifiable, Hashable
{
    let id                  = UUID()
    var NameOfItem          : String
    var dynamicData         : [staticData]?
}

let Child1 = staticData(NameOfItem: "Child1")
let Child2 = staticData(NameOfItem: "Child2")
let Child3 = staticData(NameOfItem: "Child3")

let parent1 = staticData(NameOfItem: "parent1", dynamicData: [Child1, Child2, Child3])
let parent2 = staticData(NameOfItem: "parent2", dynamicData: [Child1, Child2])
let parent3 = staticData(NameOfItem: "parent3")


struct ContentView: View
{
    @State var items: [staticData] = [parent1, parent2, parent3]
    
    @State var expanded = false
    
    var body: some View
    {
        VStack
        {
            HStack
            {
                Spacer()
                
                Button("Expand parent1")
                {
                    withAnimation { self.expanded = true }
                }
                
                Spacer()
                
                Button("Collapse parent1")
                {
                    withAnimation { self.expanded = false }
                }
                
                Spacer()
            }
            
            List {
                ForEach(Array(items.enumerated()), id: \.1) { i, item in
                    if i == 0 {
                        DisclosureGroup(isExpanded: $expanded )
                        {
                            ForEach(item.dynamicData ?? []) { child in
                                Text(child.NameOfItem)
                            }
                        } label: {
                            Text(item.NameOfItem)
                                .onTapGesture
                                {
                                    print(item.id)
                                    print(item.NameOfItem)
                                    print(item.dynamicData?.count ?? 0)
                                }
                        }
                    } else if item.dynamicData != nil {
                        DisclosureGroup
                        {
                            ForEach(item.dynamicData ?? []) { child in
                                Text(child.NameOfItem)
                            }
                        } label: {
                            Text(item.NameOfItem)
                                .onTapGesture
                                {
                                    print(item.id)
                                    print(item.NameOfItem)
                                    print(item.dynamicData?.count ?? 0)
                                }
                        }
                    } else {
                        Text(item.NameOfItem)
                    }
                }
            }
        }
    }
}