【问题标题】:sheet inside ForEach doesn't loop over id'sForEach 内的工作表不会遍历 id
【发布时间】:2021-08-20 08:50:04
【问题描述】:

每个链接都有一个id,当我点击按钮时得到的输出是错误的id,它在前4个按钮上重复第一个id,然后其他的都是随机的。

struct PricesList: View {
    @ObservedObject var viewModel = ViewModel()
    @State var isSheetPresented = false
    
    var body: some View{
        NavigationView{
            ScrollView {
                LazyVGrid(columns: gridLayout, spacing: 15, content:  {
                    ForEach(viewModel.items, id: \.id) { item in
                        VStack(alignment: .leading, spacing: 5){
                            Button(action: {
                                self.isSheetPresented.toggle()
                            }, label: {
                                Image(item.imageUrl)
                                    .resizable()
                                    .scaledToFit()
                                    .padding(10)
                            }).sheet(isPresented: $isSheetPresented, content: {
                                WebView(url: item.link)
                            })
                            
                        }//:VSTACK
                        .scaledToFit()
                        .padding()
                        .background(Color.white.cornerRadius(12))
                        .background(RoundedRectangle(cornerRadius: 12).stroke(Color.gray, lineWidth: 1))
                    }//: LOOP FOR EACH
                }).padding(5)
                .onAppear(perform: {
                    viewModel.loadData()
                    viewModel.postData()
                })
            }
            .navigationBarHidden(true)
        }//: NAVIGATION VIEW
    } //: BODY
}

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    列表中的每个项目都有sheet,并且所有项目都获得isSheetPresented 值。将显示哪一个是未定义的

    相反,您需要存储 selectedItem 并将其传递给单个 sheet,如下所示:

    struct ContentView: View {
        @ObservedObject var viewModel = ViewModel()
    
        @State var selectedItem: Item?
        
        var body: some View{
            NavigationView{
                ScrollView {
                    LazyVGrid(columns: gridLayout, spacing: 15, content:  {
                        ForEach(viewModel.items, id: \.id) { item in
                            VStack(alignment: .leading, spacing: 5){
                                Button(action: {
                                    selectedItem = item
                                }, label: {
                                    Image(item.imageUrl)
                                        .resizable()
                                        .scaledToFit()
                                        .padding(10)
                                })
    
                            }//:VSTACK
                            .scaledToFit()
                            .padding()
                            .background(Color.white.cornerRadius(12))
                            .background(RoundedRectangle(cornerRadius: 12).stroke(Color.gray, lineWidth: 1))
                        }//: LOOP FOR EACH
                    }).padding(5)
                    .onAppear(perform: {
                        viewModel.loadData()
                        viewModel.postData()
                    })
                    .sheet(item: $selectedItem, content: { selectedItem in
                        WebView(url: selectedItem.link)
                    })
                }
                .navigationBarHidden(true)
            }//: NAVIGATION VIEW
        } //: BODY
    }
    

    【讨论】:

    • 嗨!我收到两个错误,@State var selectedItem: Item? 上的“无法在范围内找到类型 'Item'”和 .sheet 上的“无法在范围内找到 '$selectedItem'”
    • @RafaelaSantos 我使用了Item,因为我不知道你的viewModel.items 的实际班级,只需将其替换为你自己的
    • @RafaelaSantos 你能做到吗?您需要打开您的ViewModel 声明,查看@Published var items: [*YourItemClass*] 并将我的代码中的Item 替换为*YourItemClass*
    • 我一直在想办法,你能看看吗?这是我的 swiftUI 文件,它正在解析来自 npoint 链接 github.com/rafaela466/iosTest/blob/main/testing 的 json 数据
    • @RafaelaSantos 你的PrecosLista 需要是Identifiable 才能在工作表中使用它。你已经有了id,所以剩下的就是添加对协议的一致性,就像这样:struct PrecosLista: Decodable, Identifiable {
    猜你喜欢
    • 2020-11-15
    • 2019-05-14
    • 1970-01-01
    • 2016-08-06
    • 2020-11-28
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多