swiftui - 如何在 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()
}
}
解决方案
推荐阅读
- node.js - 等待关键字不停止执行
- jenkins - 如何获取或将其他工作构建持续时间传递给当前工作
- javascript - 从父级删除子时使用类和 id 的问题
- javascript - typescript 从数组的前端和后端获取 X 元素
- database - 企业版 Neo4j 性能不佳
- java - Servlet = 服务器小程序?
- mysql - Mysql:标记从转储表中插入的行
- python - 如何检查一行是否包含列表中的字符串并打印匹配的字符串
- react-native - StackNavigator 菜单未在左侧对齐
- python - 我们如何删除 LinkedList 中的节点,只给它一个指向节点之后的指针?