簡體   English   中英

在 SwiftUI 中為 10 多個項目的列表視圖創建導航鏈接數組

[英]Creating Array of Navigation Links for a List View for More than 10 Items in SwiftUI

有沒有辦法創建一個項目數組來填充所有都有導航鏈接的列表視圖? 我試圖使下面的代碼工作,以便填充列表視圖的每個項目都可以鏈接到另一個具有列表中項目描述的視圖。 在此先感謝您的任何幫助!

    import SwiftUI
    
    struct Restaurant: Identifiable {
    var id = UUID()
    var name: String
    //var destination: 
}

struct RestaurantRow: View {
    var restaurant: Restaurant

    var body: some View {
        Text("Come and eat at \(restaurant.name)")
    }
}

struct Parts: View {
    
    var body: some View {
        
        let restaurants = [Restaurant(name: "1"), Restaurant(name: "2"), Restaurant(name: "3"), Restaurant(name: "4"), Restaurant(name: "5"), Restaurant(name: "6"), Restaurant(name: "7"), Restaurant(name: "8"), Restaurant(name: "9"), Restaurant(name: "10"), Restaurant(name: "11"), Restaurant(name: "12"), Restaurant(name: "1"), Restaurant(name: "2"), Restaurant(name: "3"), Restaurant(name: "4"), Restaurant(name: "5"), Restaurant(name: "6"), Restaurant(name: "7"), Restaurant(name: "8"), Restaurant(name: "9"), Restaurant(name: "10"), Restaurant(name: "11"), Restaurant(name: "12")]

        return List(restaurants) { restaurant in
            RestaurantRow(restaurant: restaurant)
        }
        .listStyle(GroupedListStyle())
        .environment(\.horizontalSizeClass, .regular)

    }
}

struct Parts_Previews: PreviewProvider {
    static var previews: some View {
        Parts()
    }
}

這是你要找的嗎?

import SwiftUI
 
 struct Restaurant: Identifiable, Hashable {
    var id = UUID()
    var name: String
 }

struct RestaurantRow: View {
 var restaurant: Restaurant

 var body: some View {
     Text("Come and eat at \(restaurant.name)")
 }
}

struct Parts: View {
    
    let restaurants = [Restaurant(name: "1"), Restaurant(name: "2"), Restaurant(name: "3"), Restaurant(name: "4"), Restaurant(name: "5"), Restaurant(name: "6"), Restaurant(name: "7"), Restaurant(name: "8"), Restaurant(name: "9"), Restaurant(name: "10"), Restaurant(name: "11"), Restaurant(name: "12"), Restaurant(name: "1"), Restaurant(name: "2"), Restaurant(name: "3"), Restaurant(name: "4"), Restaurant(name: "5"), Restaurant(name: "6"), Restaurant(name: "7"), Restaurant(name: "8"), Restaurant(name: "9"), Restaurant(name: "10"), Restaurant(name: "11"), Restaurant(name: "12")]
 
 var body: some View {
    NavigationView {
        List {
            ForEach(restaurants, id: \.self) { restaurant in
                NavigationLink(
                    destination:
                        Text("NEXT VIEW GOES HERE")
                    ,
                    label: {
                        RestaurantRow(restaurant: restaurant)
                    })
            }
        }
        //.environment(\.horizontalSizeClass, .regular)
        //.listStyle(GroupedListStyle()) // Optional
        //.navigationTitle("Title goes here") // Optional
        //.navigationBarTitleDisplayMode(.inline) // Optional
        .navigationBarHidden(true) //Optional
    }

 }
}

struct Parts_Previews: PreviewProvider {
 static var previews: some View {
     Parts()
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM