This is obviously a user error, but I've been working on different possibilities for a couple of days and nothing works. The problem is my Section in the following code doesn't expand or collapse when I click on the chevron:
`class AstroCat { var title: String var contents: [ String ] var isExpanded: Bool
init(title: String, contents: [String], isExpanded: Bool) {
self.title = title
self.contents = contents
self.isExpanded = isExpanded
}
}
struct TestView: View {
@Binding var isShowingTargetSelection: Bool
@State var catalog: AstroCat
@State private var expanded = false
var body: some View {
NavigationStack {
List {
Section(catalog.title, isExpanded: $catalog.isExpanded) {
ForEach(catalog.contents, id: \.self) { object in
Text(object)
}
}
}
.navigationTitle("Target")
.listStyle(.sidebar)
}
}
}
#Preview {
struct TestPreviewContainer : View {
@State private var value = false
@State var catalog = AstroCat(title: "Solar System", contents: ["Sun", "Mercury", "Venus", "Earth"], isExpanded: true)
var body: some View {
TestView(isShowingTargetSelection: $value, catalog: catalog)
}
}
return TestPreviewContainer()
}`
If I change the "isExpanded: $catalog.isExpanded" to just use the local variable "expanded", then it works, so I think I have the basic SwiftUI pieces correct. But using a boolean inside of the class doesn't seem to work (the section just remains expanded or collapsed based on the initial value of the class variable).
Any hints? Am I not specifying the binding correctly? (I've tried a bunch of alternatives)
Thanks for the help, Robert