gpt4 book ai didi

具有动态数据的 SwiftUI 分层选择器

转载 作者:行者123 更新时间:2023-12-04 15:34:09 25 4
gpt4 key购买 nike

我正在尝试在 SwiftUI (XCode 11.3.1) 中对动态数据使用多个选择器。该应用程序有时会崩溃,有时会卡住或在模拟器和运行 iOS 13.3.1 的真实设备上的选择器中显示错误数据。我尝试了 this question 的答案中的建议没有成功。我做错了什么?

import SwiftUI

struct DbItem : Identifiable {
var id: Int
var name: String
}

final class DbStore : ObservableObject {

let countries: [DbItem] = [DbItem(id: 0, name: "USA"), DbItem(id: 1, name: "France")]
let citymap:[Int:[DbItem]] = [0:[DbItem(id: 10, name: "New York"), DbItem(id: 11, name: "Los Angeles"), DbItem(id: 12, name: "Dallas"), DbItem(id: 13, name: "Chicago")], 1:[DbItem(id: 20, name: "Paris"), DbItem(id: 21, name: "Nice"), DbItem(id: 22, name: "Lille")]]

@Published var cities = [DbItem]()
@Published var country : Int = -1 {
willSet {
if newValue >= 0 {
self.id = UUID()
DispatchQueue.main.async { [newValue] in
self.cities = self.citymap[newValue]!
}
}
}
}
@Published var city : Int = -1 {
didSet {
}
}
}

struct ContentView: View {
@EnvironmentObject private var store: DbStore

var body: some View {
NavigationView {
Form {
VStack {
Picker(selection: $store.country,
label: Text("Country: ")
) {
ForEach(store.countries) { country in
Text(country.name)
}
}
Picker(selection: $store.city,
label: Text("City: ")
) {
ForEach(store.cities) { city in
Text(city.name)
}
}
.disabled(store.country < 0)
}
}
}
}
}

最佳答案

使用您问题中提到的链接中提供的相同代码,我做了一些小改动以采用您需要的代码

struct ContentView: View {
@ObservedObject var model = Model()
var body: some View {
NavigationView {
Form {
Section(header: Text("Header").font(.title)) {
Picker(selection: $model.selectedContry, label: Text("Country")){
ForEach(0 ..< model.countryNemes.count){ index in
Text(self.model.countryNemes[index])
}
}
Picker(selection: $model.selectedCity, label: Text("City")){
ForEach(0 ..< model.cityNamesCount){ index in
Text(self.model.cityNames[index])
}
}
.id(model.id)
}
}.navigationBarTitle("Navigation Title")
}
}
}

请看,Form中没有VStack,只有Section!结果按预期工作。 (其余代码没有任何变化)。在真实设备上尝试代码(由于模拟器中已知的“后退按钮”错误)

enter image description here

如果您对其余代码有疑问,请看这里

import Foundation
import SwiftUI

struct Country: Identifiable {
var id: Int = 0
var name: String
var cities: [City]
}

struct City: Identifiable {
var id: Int = 0
var name: String
}

class Model: ObservableObject {
let countries: [Country] = [Country(id: 0, name: "USA", cities: [City(id: 0, name: "New York"),City(id: 1, name: "Los Angeles"),City(id: 2, name: "Dallas"),City(id: 3, name: "Chicago")]),Country(id: 1, name: "France", cities: [City(id: 0, name: "Paris")])]

@Published var selectedContry: Int = 0 {
willSet {
print("country changed", newValue, citySelections[newValue] ?? 0)
selectedCity = citySelections[newValue] ?? 0
id = UUID()
}
}
@Published var id: UUID = UUID()
@Published var selectedCity: Int = 0 {
willSet {
DispatchQueue.main.async { [newValue] in
print("city changed", newValue)
self.citySelections[self.selectedContry] = newValue
}
}
}
var countryNemes: [String] {
countries.map { (country) in
country.name
}
}
var cityNamesCount: Int {
cityNames.count
}
var cityNames: [String] {
countries[selectedContry].cities.map { (city) in
city.name
}
}

private var citySelections: [Int: Int] = [:]
}

关于具有动态数据的 SwiftUI 分层选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60329428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com