简体   繁体   中英

SwiftUI list showing an array is not updated

In my app in SwiftUI, there is a list showing all items in an array. When I click on one item, its details are shown and can be modified. Those changes are stored in the array, but when I go back to the list view, the changes are only reflected after I made a change to that list array, like adding or moving an item. Can I make this list refresh when it re-appears?

My main view looks like this:

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
}

The detail view looks like this:

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)
    }
}

The class Item is Identifiable and Equatable and only holds the necessary members, the class ItemStore only holds an array of Items.

i just tried this (had to enhance code so it was compilable at all) and it works:

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM