Xcode SwiftUI中的绑定在循环内无法工作
根据我的理解,我编写了以下代码,用于展开/折叠列表中的一个部分Xcode SwiftUI中的绑定在循环内无法工作,xcode,swiftui,combine,Xcode,Swiftui,Combine,根据我的理解,我编写了以下代码,用于展开/折叠列表中的一个部分 struct WORKING_CollapsableListView: View { @State var sectionExpansionStates = [true, true, true] var body: some View { VStack { List { Section(header: CollapsableSectionHeader(expansionState:
struct WORKING_CollapsableListView: View {
@State var sectionExpansionStates = [true, true, true]
var body: some View {
VStack {
List {
Section(header: CollapsableSectionHeader(expansionState: self.$sectionExpansionStates[0])) {
if self.sectionExpansionStates[0] {
ForEach(0..<10) { item in
Text("\(item) is \(self.sectionExpansionStates[0] ? "Expanded" : "Collapsed")")
.frame(height: self.sectionExpansionStates[0] ? 10 : 10)
}
}
}
Section(header: CollapsableSectionHeader(expansionState: self.$sectionExpansionStates[1])) {
if self.sectionExpansionStates[1] {
ForEach(0..<10) { item in
Text("\(item) is \(self.sectionExpansionStates[1] ? "Expanded" : "Collapsed")")
.frame(height: self.sectionExpansionStates[1] ? 10 : 10)
}
}
}
Section(header: CollapsableSectionHeader(expansionState: self.$sectionExpansionStates[2])) {
if self.sectionExpansionStates[2] {
ForEach(0..<10) { item in
Text("\(item) is \(self.sectionExpansionStates[2] ? "Expanded" : "Collapsed")")
.frame(height: self.sectionExpansionStates[2] ? 10 : 10)
}
}
}
}
}
}
}
struct CollapsableSectionHeader: View {
@Binding var expansionState: Bool
var body: some View {
Button(action: {
self.expansionState.toggle()
}) {
Text("HEADER: \(expansionState ? "Expanded" : "Collapsed")")
.bold()
}
}
}
这是由于每个对象的静态范围。。。正如我在这里所经历的,这是SwiftUI中最混乱的概念。。无论如何-解决方案是为节使用显式模型的动态容器 这里是代码的简化工作演示(但是这个想法应该很容易被未提供的组件采用) 使用Xcode 11.4/iOS 13.4进行测试
//节的简单演示模型
结构截面模型:可识别{
让id:Int
var=true
}
结构TestCollapsableListView:视图{
//具有模型的动态容器,状态已触发
@State var sections=[SectionModel(id:0)、SectionModel(id:1)、SectionModel(id:2)]
var body:一些观点{
VStack{
名单{
ForEach(sections){section in
节(标题:按钮(“节\(节.id)”){self.sections[Section.id].expanded.toggle()}){
如果第.1节已展开{
ForEach(0..谢谢!哇。这真是难以置信。在我的实际代码中,我使用的是类似的状态模型,但我现在看到的唯一区别是,我使用的是ForEach中的索引,而不是部分。id
,这很有效!这太微妙了,我自己无法理解。
struct NOT_WORKING_CollapsableListView: View {
@State var sectionExpansionStates = [true, true, true]
var body: some View {
VStack {
List {
ForEach(0 ..< 3) { section in
Section(header: CollapsableSectionHeader(expansionState: self.$sectionExpansionStates[section])) {
if self.sectionExpansionStates[section] {
ForEach(0..<10) { item in
Text("\(item) is \(self.sectionExpansionStates[section] ? "Expanded" : "Collapsed")")
.frame(height: self.sectionExpansionStates[section] ? 10 : 10)
}
}
}
}
}
}
}
}
// simple demo model for sections
struct SectionModel: Identifiable {
let id: Int
var expanded = true
}
struct TestCollapsableListView: View {
// dynamic container with model, state is triggered
@State var sections = [SectionModel(id: 0), SectionModel(id: 1), SectionModel(id: 2)]
var body: some View {
VStack {
List {
ForEach(sections) { section in
Section(header: Button("Section \(section.id)") { self.sections[section.id].expanded.toggle() }) {
if section.expanded {
ForEach(0..<10) { item in
Text("\(item) is \(section.expanded ? "Expanded" : "Collapsed")")
.frame(height: section.expanded ? 10 : 10)
}
}
}
}
}
}
}
}