Arrays SwiftUI-列出嵌套数组中的元素

Arrays SwiftUI-列出嵌套数组中的元素,arrays,swiftui,Arrays,Swiftui,我试图在列表视图中显示顶级数组中的元素。数据模型的构建方式是,它是一个事件数组,然后在该数组中有一个与单个事件关联的场馆数组 在主视图中,我知道如何通过索引显示单个事件标题,但不知道如何使用ForEach列出所有不同的事件 Passports.swift(数据模型) 斯威夫特护照 import SwiftUI struct PassportsView: View { var model = Passports.all() var body: some View {

我试图在列表视图中显示顶级数组中的元素。数据模型的构建方式是,它是一个事件数组,然后在该数组中有一个与单个事件关联的场馆数组

在主视图中,我知道如何通过索引显示单个事件标题,但不知道如何使用ForEach列出所有不同的事件

Passports.swift(数据模型)

斯威夫特护照

import SwiftUI

struct PassportsView: View {

    var model = Passports.all()

    var body: some View {
        NavigationView {
            ForEach(self.model) { item in
                NavigationLink (destination: PassportDetails(passportTitle: item.passportTitle, venues: item.venues, venueProd: []) ) {
                HStack {
                    VStack(alignment: .leading) {
                        Text(item.passport[0].passportTitle)
                            .fontWeight(.semibold)
                            .foregroundColor(Color.white)
                        Text(item.passport[0].passportDates)
                            .foregroundColor(Color.white)
                    }.frame(width: 400, height: 120)
                        .background(Color("wPurple"))
                        .cornerRadius(6)
                }
              }.padding(.horizontal) 
            }
        }
    }
}
护照详情

struct PassportDetails: View {

var passportTitle: String
var venues: [Venue]
var venueProd: [venueItem]

var body: some View {

    List {
        ForEach(self.venues) { gc in
            Section(header: Text(gc.title)) {
                ForEach(gc.venueItems, id: \.title) { gi in
                    GeometryReader { geometry in
                        VStack(alignment: .leading) {
                            HStack {
                                Text(gi.title)
                                    .frame(width: geometry.size.width / 1.5, alignment: .leading)
                                    .fixedSize(horizontal: false, vertical: true)
                                    .padding(.top, 10)
                                Spacer()
                                Text("$\(gi.productPrice, specifier: "%.2f")")
                                    .multilineTextAlignment(.trailing)

                            }.padding(.top, 8)
                            HStack {
                                Text(gi.productDescription)
                                    .font(.footnote)
                                    .foregroundColor(Color.gray)
                                    .frame(width: geometry.size.width / 1.5, alignment: .leading)
                                .fixedSize(horizontal: false, vertical: true)
                                    .padding(.bottom, 8)
                                Spacer()
                            }.padding(.bottom, 8)
                        }
                    }.padding(.vertical)
                }
            }
        }
    }.listStyle(GroupedListStyle())
        .navigationBarTitle(Text(passportTitle), displayMode: .inline)
}
}

显然,“Text(item.passport[0].passportTitle)”将显示数组中的第一个项,但我希望在顶部数组中显示所有passportTitle和passportDate,并使用NavigationLink将信息传递到详细信息视图。

根据您的描述,您需要一个两级数据模型和视图层次结构:

  • 事件列表(由
    Passport
    struct表示)
  • 每个活动的场馆列表(由
    场馆
    结构表示)
  • 您当前的数据模型为三级(
    Passport
    包含
    Passport
    包含
    场馆

    如果我们删除无关的
    Passports
    结构,我们可以清理静态
    .all()
    函数和
    PassportsView
    结构

    extension Passport {
        static func all() -> [Passport] {
            return [
                Passport (
                    id: 1001,
                    passportPremium: false,
                    passportActive: true,
                    passportTitle : "Wine Festival",
                    passportDates: "October 20 - November 3, 2020",
                    venues: [
                        Venue (
                            title: "Bavaria Holiday Kitchen",
                            venueArea: "Germany Pavilion",
                            venueItems: [
                                venueItem (
                                    title: "Potato Dumpling",
                                    productDescription: "Potato Dumpling with Mushroom Sauce",
                                    productPrice: 0.00,
                                    productType: "Food",
                                    newStatus: false,
                                    diningPlan: false,
                                    kidFriendly: true,
                                    vegetarian: false,
                                    glutenFree: false,
                                    featuredProduct: false,
                                    containsAlcohol: false
                                )
                        ])
                ])
            ]
        }
    }
    
    以下是更新后的视图:

    struct PassportsView: View {
    
        var model = Passport.all()
    
        var body: some View {
            NavigationView {
                ForEach(self.model) { passport in
                    NavigationLink (destination: PassportDetails(passportTitle: passport.passportTitle, venues: passport.venues, venueProd: []) ) {
                    HStack {
                        VStack(alignment: .leading) {
                            Text(passport.passportTitle)
                                .fontWeight(.semibold)
                                .foregroundColor(Color.white)
                            Text(passport.passportDates)
                                .foregroundColor(Color.white)
                        }.frame(width: 400, height: 120)
                            .background(Color("wPurple"))
                            .cornerRadius(6)
                    }
                  }.padding(.horizontal)
                }
            }
        }
    }
    
    struct PassportsView: View {
    
        var model = Passport.all()
    
        var body: some View {
            NavigationView {
                ForEach(self.model) { passport in
                    NavigationLink (destination: PassportDetails(passportTitle: passport.passportTitle, venues: passport.venues, venueProd: []) ) {
                    HStack {
                        VStack(alignment: .leading) {
                            Text(passport.passportTitle)
                                .fontWeight(.semibold)
                                .foregroundColor(Color.white)
                            Text(passport.passportDates)
                                .foregroundColor(Color.white)
                        }.frame(width: 400, height: 120)
                            .background(Color("wPurple"))
                            .cornerRadius(6)
                    }
                  }.padding(.horizontal)
                }
            }
        }
    }