[英]How do I click on the items inside the list section with scrollview to navigate to detail page SwiftUI
有一個列表,其中有部分,每個部分包含項目,放在水平滾動視圖中,當嘗試使用 NavigationLink 導航到詳細信息頁面時,所有項目都被選中,我想 select 單個項目導航到詳細信息頁面。 這是我的代碼。 當我點擊列表時,它會打開多次。 我希望打開唯一選定的項目
let menu = Bundle.main.decode([ItemSection].self, from: "abc.json")
var body: some View {
List(menu) { sections in
VStack(alignment: .leading){
Text(sections.name)
HStack{
ScrollView(.horizontal, showsIndicators: false) {
Section {
HStack{
ForEach(sections.items){ allItems in
NavigationLink(destination: DetailView()){
ItemsTab(item: allItems)
}
}
}
}
}
}
}
}
}
struct ItemTab: View {
var items: Items
var body: some View {
VStack{
Image("name")
.resizable()
.cornerRadius(3)
.frame(width: 180, height: 180)
Text(items.name)
.foregroundColor( Color(red: 0 / 255, green: 38 / 255, blue: 107 / 255))
.font(.headline)
}
.padding(.top)
.padding(.bottom)
.padding(.leading, 5)
.padding(.trailing,5)
}
我已經在本地復制了您的代碼,我相信您會從列表視圖中錯過NavigationView
。 確保將列表包裝到NavigationView
中,如下所示:
NavigationView { // <-- Be sure to add this
List(menu) { sections in
VStack {
/*...*/
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.