【问题标题】:SwiftUI each item displays it own detailView when it is selected in a listSwiftUI 每个项目在列表中被选中时都会显示它自己的 detailView
【发布时间】:2021-07-26 01:08:45
【问题描述】:

我正在尝试使用 SwiftUI 让每个项目在列表中显示它自己的 detailView。但是现在,我被卡住了,因为它只为任何项目显示相同的 detailView。有人知道该怎么做吗? 这是我现在拥有的代码:

 struct PastryListView: View {
@State private var isShowingDetailView = false

@State private var selectedPastry : Pastry?

@State private var selection: Int? = nil
var body: some View {
    ZStack {
    NavigationView {
        
        List(MockData.pastries) { Pastry in
            HStack {
                Image(Pastry.image)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 180, height: 200)
                VStack {
                    Text(Pastry.name)
                        .font(Font.custom("DancingScript-Regular", size: 30))
                        .fontWeight(.medium)
                       
                }
                .padding(.leading)
            }
        
            .onTapGesture {
                selectedPastry = Pastry
                isShowingDetailView = true
            
            }
           
          
           
                    
        }
        .navigationTitle("???? Pastries")
    }
        
        if isShowingDetailView {                Pastry2DetailView(isShowingDetailView2: $isShowingDetailView, pastry: MockData.samplePastry2)


        }
      
    }
}  }

【问题讨论】:

    标签: swift xcode listview swiftui


    【解决方案1】:

    有很多方法可以实现你想要的,这只是一种方法:

    import SwiftUI
    
    @main
    struct TestApp: App {
        var body: some Scene {
            WindowGroup {
                PastryListView()
            }
        }
    }
    
    struct Pastry: Identifiable {
       var id: String = UUID().uuidString
       var name: String
       var image: UIImage
    }
    
    struct PastryListView: View {
        @State private var pastries: [Pastry] = [
            Pastry(name: "pastry1", image: UIImage(systemName: "globe")!),
            Pastry(name: "pastry2", image: UIImage(systemName: "info")!)]
        
        var body: some View {
            NavigationView {
                List(pastries) { pastry in
                    NavigationLink(destination: PastryDetailView(pastry: pastry)) {
                        HStack {
                            Image(uiImage: pastry.image)
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: 80, height: 80)
                            VStack {
                                Text(pastry.name)
                                    .font(Font.custom("DancingScript-Regular", size: 30))
                                    .fontWeight(.medium)
                            }
                            .padding(.leading)
                        }
                    }
                }.navigationTitle("? Pastries")
            }.navigationViewStyle(StackNavigationViewStyle())
        }
    }
    
    struct PastryDetailView: View {
        @State var pastry: Pastry
        var body: some View {
            Text("???? " + pastry.name + " ????")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 2019-10-28
      相关资源
      最近更新 更多