Creating a series of master detail lists from a single JSON file in SwiftUI

前端 未结 2 1993
伪装坚强ぢ
伪装坚强ぢ 2021-01-24 22:40

I\'m trying to work through understanding how I can make data flow nicely through an app I\'m building. I just want a basic master detail view where it starts with a list of all

2条回答
  •  灰色年华
    2021-01-24 23:25

    Your CityModel and TownModel need to conform to Identifiable, just add an id to them like you did in UserModel.

    Than you need to edit your UserList NavigationLink:

    NavigationLink(destination: CityList(cities: user.cities)) {
        Text(user.firstName)
    }
    

    The Navigation is now like this: UserList -> CityList -> TownList

    CityList:

    struct CityList: View {
    
        var cities: [CityModel]
    
        var body: some View {
            List (cities) { city in
                NavigationLink(destination: TownList(towns: city.towns)) {
                    Text(city.name)
                }
            }
        }
    }
    

    TownList:

    struct TownList: View {
    
        var towns: [TownModel]
    
        var body: some View {
            List (towns) { town in
                Text(town.name)
            }
        }
    }
    

    I hope that helps, in my test project it works!

提交回复
热议问题