Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在swiftUI中将我的数据添加到这个可扩展列表中?_Swiftui_Swiftui List - Fatal编程技术网

如何在swiftUI中将我的数据添加到这个可扩展列表中?

如何在swiftUI中将我的数据添加到这个可扩展列表中?,swiftui,swiftui-list,Swiftui,Swiftui List,我想在swiftUI中创建一个可扩展列表。我找到了这个解决方案,它几乎就是我想要的 我想补充的是: 1.在第一个ForEach中,使用来自@Published属性包装的自定义数据 2.我可以从第二个ForEach中获取索引,这样我就可以将图标的图像更改为选中每个子项,就像它的父项一样 以下是应用程序的图像: 我的代码是: import SwiftUI struct DemoDisclosureGroups: View { @State var items: [Bookmark] =

我想在swiftUI中创建一个可扩展列表。我找到了这个解决方案,它几乎就是我想要的

我想补充的是:

1.在第一个
ForEach
中,使用来自
@Published
属性包装的自定义数据

2.我可以从第二个ForEach中获取
索引
,这样我就可以将图标的图像更改为选中每个子项,就像它的父项一样

以下是应用程序的图像:

我的代码是:

import SwiftUI

struct DemoDisclosureGroups: View {
    @State var items: [Bookmark] = [.example1, .example2, .example3]
    @State private var flags: [Bool] = [false, false, false]
    @StateObject var appState = AppState()

var body: some View {
    List {
        // I want to use appState.allBookmark instead of items in This ForEach
        ForEach(Array(items.enumerated()), id: \.1.id) { i, group in
            DisclosureGroup(isExpanded: $flags[i]) {
                // And in this ForEach get an index so I can change child .isSee property
                ForEach(group.items ?? []) { item in
                    Label(item.name, systemImage: item.isSee ? "checkmark.square.fill" : item.icon)
                        .onTapGesture {
                            withAnimation {
                              // use index to change isSee property
                            }
                        }
                }
            } label: {
                Label(group.name,systemImage: group.isSee ? "checkmark.square.fill" : group.icon)
                    .contentShape(Rectangle())
                    .onTapGesture {
                        withAnimation {
                            items[i].isSee.toggle()
                        }
                    }
            }
        }
    }
    .listStyle(InsetGroupedListStyle())
}
}


struct Bookmark: Identifiable {

let id = UUID()
let name: String
let icon: String
var isSee = false
var items: [Bookmark]?



  //  some example websites
    static let apple = Bookmark(name: "Apple", icon: "square")
    static let bbc = Bookmark(name: "BBC", icon: "square")
    static let swift = Bookmark(name: "Swift", icon: "square")
    static let twitter = Bookmark(name: "Twitter", icon: "square")

// some example groups
static let example1 = Bookmark(name: "Favorites", icon: "square", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
static let example2 = Bookmark(name: "Recent", icon: "square", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
static let example3 = Bookmark(name: "Recommended", icon: "square", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
}

// get this Data to the expandable list instead of defalut data :
class AppState : ObservableObject {
    @Published var allBoomark : [Bookmark] = [
                            Bookmark(name: "Italy", icon: "square",items: italyCity),
                            Bookmark(name: "Spania", icon: "square",items: spainCity),
                            Bookmark(name: "England", icon: "square",items: englandCity),
                            ]
}

let italyCity = [
            Bookmark(name: "Rome", icon: "square"),
            Bookmark(name: "Milan", icon: "square"),
            Bookmark(name: "Turin", icon: "square"),
]

let spainCity = [
            Bookmark(name: "Madrid", icon: "square"),
            Bookmark(name: "Barcelona", icon: "square"),
            Bookmark(name: "Valencia", icon: "square"),
]

let englandCity = [
            Bookmark(name: "London", icon: "square"),
            Bookmark(name: "Manchester", icon: "square"),
            Bookmark(name: "Liverpool", icon: "square"),
]

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        DemoDisclosureGroups()
    }
}

@loremipsum在这种情况下,使用
ForEach(group.items.index){item In…}更安全
这是实现这一点的快速方法。