我想显示一些数据的类别和子类别。我有从 json 获得的数据,当一个类别没有子类别时parent = 0
,如果它不为零,则可以理解该类别具有子类别。
然后从护理人员列表中我希望有一个NavigationLink
根据 的值parent
。
我怎样才能NavigationLink
有条件?
如果类别没有子类别,则必须转到Product ()
视图,否则必须转到Scategory
视图
类似的东西,要添加到Navigation Link
if parent == 0 {
Product()
} else {
Scategory()
}
基本示例代码
struct ContentView: View {
private let cats = [
"Category 1", "Category 2"
]
//Here I am assuming this value, when changing to zero you must change the destination of `navigationLInk`
var parent = 20
var body: some View {
NavigationView {
List(cats, id: \.self) { item in
//Here, how can I add a conditional to the `NavigationLink` using the value of` parent`
NavigationLink(destination: Scategory(item: item)) {
Text(item)
}
}.navigationBarTitle("Category")
}
}
}
struct Scategory: View {
let item: String
var body: some View {
VStack {
Text("Subcategory View \(item)")
.font(.largeTitle)
}
}
}
struct Product: View {
let item: String
var body: some View {
VStack {
Text("Produc View \(item)")
.font(.largeTitle)
}
}
}