SwiftUI - 当每个列表视图的内容取决于前一个列表视图中的选择时的最佳方法?

SwiftUI - Best approach for when each listview's contents is dependent upon the selection in the previous listview?

我有 3 个简单数组:

var myContintents = [Europe,Africa]
var myCountries = [UK, France, Senegal]
var myCities = [London, Birmingham, Paris, Dakar]

每个数组填充一个列表视图,带有指向下一个列表视图的导航链接

Listview 1 = Contintents
Listview 2 = Countries
Listview 3 = Cities

但是,我在如何让这个受抚养人方面遇到了问题

例如,

如果在 Listview1 上选择 'Europe',则 Listview2 应该只包含英国和法国(不包括塞内加尔,因为塞内加尔不在欧洲)

如果在 Listview 2 上选择了 'France',那么 Listview 3 应该只包含 Paris

欢迎就如何处理此问题提出任何建议

谢谢

您应该学习如何创建自己的自定义类型,对于这种情况,以下内容应该是合适的

struct Continent {
    let name: String
    let countries: [Country]
}

struct Country {
    let name: String
    let cities: [City] //Or skip the last struct and make this a [String]
}

struct City {
    let name: String
}

Now you have an array of Continent in your first list view and when one continent is selected then fill the next one with the countries on the countries array 属性

这是一个小例子

let continents = [
    Continent(name: "Europe",
              countries: [
                Country(name: "UK",
                        cities: [
                            City(name: "London"),
                            City(name: "Birmingham")
                        ]),
                Country(name: "France",
                        cities: [
                            City(name: "Paris")
                        ])
              ])
]