Search code examples
swiftswiftuinavigationtableviewnavigationview

SwiftUI link each cell to new View


I'm new to SwiftUI, I want to know how can I link my list if someone toggle a cardView to a new SwiftUI view like how I used to do with storyboard and tableView for each cell I toggle

struct ScrollViewCont: View {
   
    var body: some View {

        ScrollView(.horizontal){

            HStack{

                ForEach(tabs) { sections in
                    GeometryReader { geometry in
                        ScrollViewTabs(
                            tabText: sections.name,
                            tabImage: sections.thumbnail
                        )
                        .rotation3DEffect(Angle(degrees: (Double(geometry.frame(in: .global).minX) - 210) / -20), axis: (x: 0, y: 1.0, z: 0))
                    }
                    .frame(width: 200, height: 250)
                    .padding()
                }         
            }
        }
    }
}

Solution

  • Here is an example code, that allows you to present the various ScrollViewTabs, then upon a tap "go to" another view.

    struct ContentView: View {
        var tabs = [
            TabSection(name: "الحروف العربية", thumbnail: "folder", SubView: "test"),
            TabSection(name: "الحروف الانجليزية", thumbnail: "person", SubView: "test2"),
            TabSection(name: "الحروف الانجليزية", thumbnail: "globe", SubView: "test2"),
            TabSection(name: "الحروف الانجليزية", thumbnail: "info", SubView: "test2"),
            TabSection(name: "الحروف الانجليزية", thumbnail: "house", SubView: "test2")
        ]
        
        @State private var selections: [TabSection] = []
        
        var body: some View {
            NavigationStack(path: $selections) {
                ScrollView(.horizontal){
                    HStack{
                        ForEach(tabs) { section in
                            GeometryReader { geometry in
                                ScrollViewTabs(tab: section).border(.green)
                                    .rotation3DEffect(Angle(degrees: (Double(geometry.frame(in: .global).minX) - 210) / -20), axis: (x: 0, y: 0, z: 1.0))
                                    .onTapGesture {
                                        selections = [section]
                                    }
                                
                            }.frame(width: 200, height: 250).padding()
                        }
                    }
                }
                .navigationDestination(for: TabSection.self) { tab in
                    OtherView(tab: tab)
                }
            }
        }
    }
    
    // for testing
    struct OtherView: View {
        var tab: TabSection
        
        var body: some View {
            Text("OtherView " + tab.name)
        }
    }
    
    // for testing
    struct ScrollViewTabs: View {
        @State var tab: TabSection
        
        var body: some View {
            Label(tab.name, systemImage: tab.thumbnail)
        }
    }
    
    struct TabSection: Identifiable, Hashable {
        let id = UUID()
        var name: String
        var thumbnail: String
        var SubView: String
    }