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

问题描述

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

SwiftUI 2.0 List with children - how to make the tappable area of the disclosure button cover the whole list item

我想添加什么,是:

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

2.我可以从第二个 ForEach 中获取 index,这样我就可以将图标的图像更改为检查每个孩子,就像它的父母一样。

这是应用程序的图像:

enter image description here

和我的代码

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",Bookmark.twitter])
static let example3 = Bookmark(name: "Recommended",Bookmark.twitter])
}

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

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

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

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

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)