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)
}
}
}
}
}
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)