多张工作表用于ForEach循环

问题开始:Int:String:“Baltimore”:“John”:“Mike”:DataArray(id:“Susan”:“Oliver”:False:isFavorite:Text(“\(data.id)”)
提问开始:

我需要你的帮助,因为我不知道如何在我的ForEach循环中显示多张工作表。我想在每次单击我的ForEach项目时显示我的DetailView()。我相信NavigationLink可能会有所帮助,但我想坚持用一张纸来展示。请帮助我解决我的问题。提前谢谢。

import Foundation
import SwiftUI

struct DataArray: Identifiable {
    let id: Int
    let cities: String
    let name1: String
    let name2: String
    
    let isBookMark: Bool
    let isFavorite: Bool
}

public struct ListDataArray {
    static let dot = [
    DataArray(id: 1,
        cities: "Baltimore"
        name1: "John",
        name2: "Mike",
        isBookMark: False,
        isFavorite: False),
        
    DataArray(id: 2,
        cities: "Frederick"),
        name1: "Joe",
        name2: "Swift",
        isBookMark: False,
        isFavorite: False),
        
    DataArray(id: 3,
        cities: "Catonsville"
        name1: "Susan",
        name2: "Oliver",
        isBookMark: False,
        isFavorite: False),
        
    // There will be a lot of data     
    ]
}

class Prospect: ObservableObject {
    @Published var datas: [DataArray] = ListDataArray.dot

}

struct Home: View {
    @EnvironmentObject var items: Prospect
    
    var body: some View {
        List {
            ForEach(items.datas) { data in
                Button {
                    //Action
                } label: {
                    LazyHStack {
                        Text("\(data.id)")
                            .font(.title3)
                        Text(data.cities)
                            .font(.subheadline)
                    }
                    .padding()
                }
                .sheet(item: ) {
                    // Action for onDismiss
                } content: { model in
                    DetailView(data: model)
                }
                
            }
            .padding()
        }
    }
}

struct DetailView: View {
    
    let data: DataArray
    
    var body: some View {
        VStack(spacing: 10) {
            
            Text(data.cities)
            Text(data.name1)
            Text(data.name2)
            
        }
        .font(.body)
    }
}

struct Home_Previews: PreviewProvider {
    static var previews: some View {
        Home()
            .environmentObject(Prospect())
    }
}
回答开始:得票数 2

您可以使用@State变量来跟踪选择了哪个项目,并将其传递给item参数的.sheet

struct Home: View {
    @EnvironmentObject var items: Prospect
    
    @State private var sheetItem : DataArray?
    
    var body: some View {
        List {
            ForEach(items.datas) { data in
                Button {
                    sheetItem = data
                } label: {
                    LazyHStack {
                        Text("\(data.id)")
                            .font(.title3)
                        Text(data.cities)
                            .font(.subheadline)
                    }
                    .padding()
                }
                
            }
            .padding()
        }
        .sheet(item: $sheetItem) {
            // Action for onDismiss
        } content: { model in
            DetailView(data: model)
        }
    }
}
总结

以上是真正的电脑专家为你收集整理的多张工作表用于ForEach循环的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得真正的电脑专家网站内容还不错,欢迎将真正的电脑专家推荐给好友。

你可能想看:
标签: data
分享给朋友: