gpt4 book ai didi

ios - SwiftUI 中默认展开的 List 或 OutlineGroup

转载 作者:行者123 更新时间:2023-12-03 11:15:55 29 4
gpt4 key购买 nike

使用 SwiftUI 中的 List 或 OutlineGroup 如何在创建 View 时使其部分(或全部)分支默认展开。这似乎可以通过具有绑定(bind)的 DisclosureGroup 实现。
这对于恢复状态或自定义 View 以进行演示很有用。

最佳答案

OutlineGroup 的可重复使用版本,其中可扩展性受到控制。

import SwiftUI

struct NodeOutlineGroup<Node>: View where Node: Hashable, Node: Identifiable, Node: CustomStringConvertible{
let node: Node
let childKeyPath: KeyPath<Node, [Node]?>
@State var isExpanded: Bool = true

var body: some View {
if node[keyPath: childKeyPath] != nil {
DisclosureGroup(
isExpanded: $isExpanded,
content: {
if isExpanded {
ForEach(node[keyPath: childKeyPath]!) { childNode in
NodeOutlineGroup(node: childNode, childKeyPath: childKeyPath, isExpanded: isExpanded)
}
}
},
label: { Text(node.description) })
} else {
Text(node.description)
}
}
}

struct ContentView: View {
var body: some View {
List {
NodeOutlineGroup(node: data, childKeyPath: \.children, isExpanded: true)
}
}
}

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

struct FileItem: Hashable, Identifiable, CustomStringConvertible {
var id: Self { self }
var name: String
var children: [FileItem]? = nil
var description: String {
switch children {
case nil:
return "📄 \(name)"
case .some(let children):
return children.isEmpty ? "📂 \(name)" : "📁 \(name)"
}
}
}

let data =
FileItem(name: "users", children:
[FileItem(name: "user1234", children:
[FileItem(name: "Photos", children:
[FileItem(name: "photo001.jpg"),
FileItem(name: "photo002.jpg")]),
FileItem(name: "Movies", children:
[FileItem(name: "movie001.mp4")]),
FileItem(name: "Documents", children: [])
]),
FileItem(name: "newuser", children:
[FileItem(name: "Documents", children: [])
])
])

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

关于ios - SwiftUI 中默认展开的 List 或 OutlineGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62832809/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com