如何在swiftui中使用多维字典的导航链接使代码更简单,更可重用? [英] how to make the code simpler and more reusable for navigation links with multi dimension dictionary in swiftui?
问题描述
我有一个多层字典,需要用来建立导航链接. 现在,我有一个2级深度词典:
I have a multi-level dictionary that I need use to build navigation links. Now I have a 2-level depth dictionary:
let multiDimDict: [String: [[String: [String]]]]
= ["A": [["A1": ["A11", "A12"]], ["A2": ["A21", "A22"]]],
"B": [["B1": ["B11", "B12"]], ["B2": ["B21", "B22"]]]
]
我可以使用所有导航链接构建导航视图.但是,我觉得我正在以某种方式在构建导航链接中重复一些代码.问题是,如果我有一个多级字典,例如10级,我认为构建10个不同的子视图生成器以构建完整的导航视图是不明智的.知道如何减少代码吗?
I can build the navigation view with all navigation links. However, I feel that I am somehow repeating some code in building navigation links. The question is, if I have a many-level dictionary, say 10-level, I don't think it's wise to build 10 different child-view generators in order to build the complete navigation view. Any idea how to reduce the code?
struct PlayingWithMultiDimNavLink: View {
let multiDimDict: [String: [[String: [String]]]]
= ["A": [["A1": ["A11", "A12"]], ["A2": ["A21", "A22"]]],
"B": [["B1": ["B11", "B12"]], ["B2": ["B21", "B22"]]]
]
var body: some View {
NavigationView{
List {
ForEach(multiDimDict.keys.sorted(), id: \.self) { key in
NavigationLink(destination: GenerateChildView(key: key, dict: self.multiDimDict)){
Text(key)
}
}
}
}
}
}
这是:first子视图生成器:
This is the :first child view generator:
struct GenerateChildView: View {
var key: String
var dict: [String: [[String: [String]]]]
var infoList: [[String: [String]]]
init(key: String, dict: [String: [[String: [String]]]] ){
self.key = key
self.dict = dict
self.infoList = dict[key]!
}
var body: some View {
List{
ForEach(infoList, id: \.self){ info in
self.getSomeView(infoList: self.infoList)
}
}
}
func getSomeView(infoList: [[String: [String]]] )-> AnyView{
let dictToUse = infoList[0]
return AnyView(
ForEach(dictToUse.keys.sorted(), id: \.self){ key2 in
NavigationLink(destination: GenerateChildView2(infoList: dictToUse[key2]!)){
Text(key2)
}
}
)
}
最后一个子视图生成器:
The last child-view generator:
struct GenerateChildView2: View {
var infoList: [String]
init(infoList: [String]){
self.infoList = infoList
}
var body: some View {
List{
ForEach(infoList, id:\.self){ content in
Text(content)
}
}
}
}
推荐答案
我将首先放弃使用字典,而使用提供更有用模型的简单递归结构:
I would start by abandoning the use of a dictionary and use a simple recursive struct that provides a more useful model:
struct Item: Identifiable {
let id = UUID()
var title: String
var children: [Item] = []
}
然后,您可以定义一个递归View
来显示此模型:
Then you can define a recursive View
to display this model:
struct NavView: View {
var item: Item
var body: some View {
NavigationView {
List {
ForEach(item.children) { child in
if child.children.isEmpty {
Text(child.title)
} else {
NavigationLink(destination: NavView(item:child)) {
Text(child.title)
}
}
}
}.navigationBarTitle(item.title)
}
}
}
然后,您可以根据需要创建层次结构,并将根传递给视图:
Then you can create your hierarchy as required and pass the root to your view:
let root = Item(title:"",children:
[Item(title:"A",children:
[Item(title:"A1", children:
[Item(title:"A11"),Item(title:"A12")]
)]),
Item(title:"B",children:
[Item(title:"B1"),Item(title:"B2")]
)]
)
NavView(item:root))
这篇关于如何在swiftui中使用多维字典的导航链接使代码更简单,更可重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!