SwiftUI doesn't update second NavigationLink destination

后端 未结 3 1423
小蘑菇
小蘑菇 2021-01-14 00:48

I have a List with rows which push a View. That view has another List which pushing another View. The original List, and the first pushed List will update when the data chan

3条回答
  •  情话喂你
    2021-01-14 01:30

    This is the test I made and is working well, everything updates as expected.

    struct User: Identifiable {
        var id: String
        var name: String
        var items: [Item]
    }
    
    struct Item: Identifiable {
        var id: String
        var name: String
        var details: [String]
    }
    
    class App: ObservableObject {
        @Published var users = [User]()
    
        init() {
        let items1 = [Item(id: UUID().uuidString, name: "item1", details: ["d1","d2"]), Item(id: UUID().uuidString, name: "item2", details: ["d3","d4"])]
        let items2 = [Item(id: UUID().uuidString, name: "item3", details: ["e1","e2"]), Item(id: UUID().uuidString, name: "item4", details: ["e3","e4"])]
        users.append(User(id: UUID().uuidString, name: "user1", items: items1))
        users.append(User(id: UUID().uuidString, name: "user2", items: items2))
        }
    }
    
    
    struct ContentView: View {
    
    @ObservedObject var app = App()
    
    var body: some View {
        NavigationView {
            List {
                ForEach(app.users) { user in
                    NavigationLink(destination: UserView(user: user)) {
                        Text(user.name)
                    }
                }
            }
        }
    }
    }
    
    
    struct UserView: View {
    @State var user: User
    
    var body: some View {
        List {
            ForEach(user.items) { item in
                NavigationLink(destination: ItemView(item: item)) {
                    Text(item.name)
                }
            }
        }
    }
    }
    
    struct ItemView: View {
    @State var item: Item
    
    var body: some View {
        List {
            ForEach(item.details, id: \.self) { detail in
                Text(detail)
            }
        }
    }
    }
    

提交回复
热议问题