Lian
Lian

Reputation: 157

Multiple sheets for ForEach Loop

I need your help since I couldn't figure out how to present multiple sheets inside my ForEach loop. I want to present my DetailView() every time I click on one of my ForEach items. I believe NavigationLink might be helpful but I would like to stick by presenting with a sheet. Please help me by solving my issue. Thanks in advance.

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())
    }
}

Upvotes: 1

Views: 264

Answers (1)

jnpdx
jnpdx

Reputation: 52625

You can use a @State variable to keep track of which item has been selected and pass that to .sheet for the item argument:

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)
        }
    }
}

Upvotes: 2

Related Questions