显示数组的SwiftUI列表未更新

在我的SwiftUI应用中,有一个列表显示了数组中的所有项目。当我单击一项时,它的详细信息就会显示出来并且可以修改。这些更改存储在数组中,但是当我返回到列表视图时,更改仅在对列表数组进行更改(例如添加或移动项目)后才会反映出来。重新显示此列表后,我可以使其刷新吗?

我的主视图如下所示:

import SwiftUI

struct ContentView: View {

    @State var items: [Item]

    var body: some View {

        NavigationView {
            List {
                ForEach(items) { item in
                    NavigationLink(destination: ItemDetailView(items: self.$items, index: self.items.firstIndex(of: item)!)) {
                        HStack {
                            VStack(alignment: .leading) {
                                Text(item.name).font(.title)
                                if item.serialNumber != nil {
                                    Text(item.serialNumber!)
                                        .font(.subheadline)
                                        .foregroundColor(.secondary)
                                }
                            }
                            Spacer()
                            Text("\(item.valueInDollars)$").font(.title)
                        }
                    }
                }
                .onDelete(perform: delete)
                .onMove(perform: move)
                Text("No more items!")
            }
            .navigationBarTitle(Text("Homepwner"), displayMode: .inline)
            .navigationBarItems(leading: EditButton(), trailing: Button(action: addItem) { Text("Add") })
        }

    }

    //... functions
}

详细视图如下所示:

import SwiftUI

struct ItemDetailView: View {

    @Binding var items: [Item]
    let index: Int

    var body: some View {
        VStack {
            VStack(alignment: .leading) {
                HStack {
                    Text("Name: ")
                    TextField("Item Name", text: $items[index].name)
                }
                //... more TextFields
            }
            .padding(.all, 8.0)
            VStack(alignment: .center) {
                //... button
                Image(systemName: "photo")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
            }
        }.navigationBarTitle(items[index].name)
    }
}

Item类是Identifiable和Equatable,仅包含必需的成员,ItemStore类仅包含Items数组。

评论
  • 云想衣裳
    云想衣裳 回复

    我只是尝试了这一点(不得不增强代码,使其完全可编译),并且它的工作原理是:

    import SwiftUI
    
    struct Item : Equatable, Identifiable, Hashable {
        var id = UUID()
        var name: String
        var serialNumber : String?
        var valueInDollars : Int = 5
    }
    
    
    
    struct ContentView: View {
    
        @State var items: [Item] = [Item(name: "hi"), Item(name: "ho")]
    
        var body: some View {
    
            NavigationView {
                List {
                    ForEach(items, id: \.self) { item in
                        NavigationLink(destination: ItemDetailView(items: self.$items, index: self.items.firstIndex(of: item)!)) {
                            HStack {
                                VStack(alignment: .leading) {
                                    Text(item.name).font(.title)
                                    if item.serialNumber != nil {
                                        Text(item.serialNumber!)
                                            .font(.subheadline)
                                            .foregroundColor(.secondary)
                                    }
                                }
                                Spacer()
                                Text("\(item.valueInDollars)$").font(.title)
                            }
                        }
                    }
    //                .onDelete(perform: delete)
    //                .onMove(perform: move)
                    Text("No more items!")
                }
                .navigationBarTitle(Text("Homepwner"), displayMode: .inline)
    //            .navigationBarItems(leading: EditButton(), trailing: Button(action: addItem) { Text("Add") })
            }
    
        }
    
        //... functions
    }
    
    
    struct ItemDetailView: View {
    
        @Binding var items: [Item]
        let index: Int
    
        var body: some View {
            VStack {
                VStack(alignment: .leading) {
                    HStack {
                        Text("Name: ")
                        TextField("Item Name", text: $items[index].name)
                    }
                    //... more TextFields
                }
                .padding(.all, 8.0)
                VStack(alignment: .center) {
                    //... button
                    Image(systemName: "photo")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
                }
            }.navigationBarTitle(items[index].name)
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }