Friday, October 9, 2026
HomeiOS Developmentios - The way to get the indices of the tapped Listing...

ios – The way to get the indices of the tapped Listing merchandise in SwiftUI

[ad_1]

I’ve a expandable Listing in SwiftUI, the Listing merchandise mannequin is outlined as(MenuItem.swift):

struct MenuItem: Identifiable {
    var id = UUID()
    var identify: String
    var picture: String
    var subMenuItems: [MenuItem]?
}
// Sub-menu objects for Espressco Machines
let espressoMachineMenuItems = [ MenuItem(name: "Leva", image: "leva-x", subMenuItems: [ MenuItem(name: "Leva X", image: "leva-x"), MenuItem(name: "Leva S", image: "leva-s") ]),
                                 MenuItem(identify: "Strada", picture: "strada-ep", subMenuItems: [ MenuItem(name: "Strada EP", image: "strada-ep"), MenuItem(name: "Strada AV", image: "strada-av"), MenuItem(name: "Strada MP", image: "strada-mp"), MenuItem(name: "Strada EE", image: "strada-ee") ]),
                                 MenuItem(identify: "KB90", picture: "kb90"),
                                 MenuItem(identify: "Linea", picture: "linea-pb-x", subMenuItems: [ MenuItem(name: "Linea PB X", image: "linea-pb-x"), MenuItem(name: "Linea PB", image: "linea-pb"), MenuItem(name: "Linea Classic", image: "linea-classic") ]),
                                 MenuItem(identify: "GB5", picture: "gb5"),
                                 MenuItem(identify: "House", picture: "gs3", subMenuItems: [ MenuItem(name: "GS3", image: "gs3"), MenuItem(name: "Linea Mini", image: "linea-mini") ])
                                ]

// Sub-menu objects for Grinder
let grinderMenuItems = [ MenuItem(name: "Swift", image: "swift"),
                         MenuItem(name: "Vulcano", image: "vulcano"),
                         MenuItem(name: "Swift Mini", image: "swift-mini"),
                         MenuItem(name: "Lux D", image: "lux-d")
                        ]

// Sub-menu objects for different gear
let otherMenuItems = [ MenuItem(name: "Espresso AV", image: "espresso-av"),
                         MenuItem(name: "Espresso EP", image: "espresso-ep"),
                         MenuItem(name: "Pour Over", image: "pourover"),
                         MenuItem(name: "Steam", image: "steam")
                        ]

In ContentView.swift, I outline a expandable Listing:

import SwiftUI

struct ContentView: View {
    @State personal var choice: Set<UUID> = []
    var physique: some View {
        
        Listing(sampleMenuItems, youngsters: .subMenuItems) { merchandise in
            // merchandise is of sort MenuItem
            HStack {
                if let picture=merchandise.picture{
                    Picture(picture)
                        .resizable()
                        .scaledToFit()
                        .body(width: 50, top: 50)
                }

                Textual content(merchandise.identify)
                    .font(.system(.title3, design: .rounded))
                    .daring()
                    .onTapGesture {
                        print("merchandise chosen=(merchandise.identify)")
                        
                    }
                Spacer()
            }
        }.listStyle(.plain)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

The Listing is 2 or 3 stage expandable:

List

Once I faucet a Listing Merchandise, I can get the merchandise identify in .onTapGesture, However what I wish to get are the indices of the tapped merchandise in sampleMenuItems Listing(take merchandise “Leva S” for instance, it’s sampleMenuItems[0].subMenuItems![0].subMenuItems![1])

How can I do that in my code?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments