gpt4 book ai didi

swift - 3 组件动态多 UIPickerView Swift

转载 作者:搜寻专家 更新时间:2023-10-31 22:54:26 25 4
gpt4 key购买 nike

我是 Swift 编程的新手(这样做是为了好玩 :))并且一直在尝试创建一个 3 组件 UIpickerview,其中每个组件都会根据先前选择的组件更改其值。我几乎让它工作但坚持使用最后一个组件 - 出现 fatal error :我的 pickerview titleForRow 方法中的索引超出范围(当它返回城市 [selectedCity].post[row] 时)非常感谢任何建议/帮助!

非常感谢。

导入 UIKit

类国家{ 变种名称:字符串 变种城市:[城市]

init(name:String, cities:[City]) {
self.name = name
self.cities = cities
}

类城市{ 变种名称:字符串 变种帖子:[字符串]

init(name:String, posts:[String]) {
self.name = name
self.posts = posts
}

ViewController 类:UIViewController、UIPickerViewDelegate、UIPickerViewDataSource {

@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var countryLbl: UILabel!

var countries = [Country]()

override func viewDidLoad() {

pickerView.delegate = self
pickerView.dataSource = self

countries.append(Country(name: "UK", cities: [City(name: "London", posts: ["L01", "L02", "L03"]),City(name: "Manchester", posts: ["M01", "M02"]), City(name: "Bristol", posts: ["B01", "B02", "B03"])]))
countries.append(Country(name: "USA", cities: [City(name: "New York", posts: ["N01"]),City(name: "Chicago", posts: ["C01", "C02"])]))
countries.append(Country(name: "China", cities: [City(name: "Beijing", posts: ["BL01", "BL02", "BL03", "BL04"]),City(name: "Shanghai", posts: ["SM01", "SM02"]), City(name: "Shenzhen", posts: ["SB01", "SB02", "SB03"]), City(name: "Hong Kong", posts: ["SH01"])]))

super.viewDidLoad()

}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

if component == 0 {
return countries.count
} else if component == 1 {
let selectedCountry = pickerView.selectedRow(inComponent: 0)
return countries[selectedCountry].cities.count
} else {
let selectedCountry = pickerView.selectedRow(inComponent: 0)
let selectedCity = pickerView.selectedRow(inComponent: 1)
return countries[selectedCountry].cities[selectedCity].posts.count
}
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return countries[row].name
} else if component == 1{
let selectedCountry = pickerView.selectedRow(inComponent: 0)
return countries[selectedCountry].cities[row].name
} else {
let selectedCountry = pickerView.selectedRow(inComponent: 0)
let selectedCity = pickerView.selectedRow(inComponent: 1)
return countries[selectedCountry].cities[selectedCity].posts[row]
}
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerView.reloadComponent(0)
pickerView.reloadComponent(1)

let selectedCountry = pickerView.selectedRow(inComponent: 0)
let selectedCity = pickerView.selectedRow(inComponent: 1)
let selectedPost = pickerView.selectedRow(inComponent: 2)
let post = countries[selectedCountry].cities[selectedCity].posts

countryLbl.text = "The right answer was: \(post) in \(selectedCountry)"

}

最佳答案

像下面这样创建国家和城市对象。将城市数组分配给城市。

class Country {

var name: String
var cities: [City]

init(name:String, cities:[City]) {
self.name = name
self.cities = cities
}
}

class City {

var name: String
var posts: [String]

init(name:String, posts:[String]) {
self.name = name
self.posts = posts
}
}

关于swift - 3 组件动态多 UIPickerView Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51824401/

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