首页 > 解决方案 > 如何在 swiftUI 中将我的数据添加到这个可扩展列表中?

问题描述

我想在 swiftUI 中制作可扩展的列表,我发现这个解决方案几乎就是我想要的。

SwiftUI 2.0 List with children - 如何使披露按钮的可点击区域覆盖整个列表项

我想添加什么,是:

1.首先ForEach使用来自@Published属性包装器的自定义数据。

2.我可以index从第二个 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()
    }
}

标签: swiftuiswiftui-list

解决方案


推荐阅读