3组件动态Multi UIPickerView Swift

时间:2018-08-13 14:13:32

标签: swift dynamic uipickerview

我是Swift编程的新手(这样做很有趣:)),并且一直试图创建3个组件的UIpickerview,每个组件都根据先前选择的组件更改其值。我几乎可以使用它了,但是卡在了最后一个组件上–出现致命错误:我的pickerview titleForRow方法中的索引超出范围(当返回返回city [selectedCity] .post [row]时) 任何建议/帮助将不胜感激!

非常感谢。

导入UIKit

国家/地区类别{     变量名称:字符串     var个城市:[城市]

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

}

阶级城市{     变量名称:字符串     var个帖子:[String]

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)"

}

}

1 个答案:

答案 0 :(得分:1)

创建国家和城市对象,如下所示。将城市数组分配给城市。

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
    }
}