1

I've created a UIPickerView using an array as my data source, I'd like to hide some items on the fourth component after I've selected an item on the third one. How can I do that?

class ViewControllerEspessuras: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    var data = [["1.50","1.60","1.67","1.74"],
                ["-10.00 Esf.","-9.00 Esf.","-8.00 Esf.","-7.00 Esf.","-6.00 Esf.","-5.00 Esf.","-4.00 Esf.","-3.00 Esf.","-2.00 Esf.","-1.00 Esf.","Plano","+1.00 Esf.","+2.00 Esf.","+3.00 Esf.","+4.00 Esf.","+5.00 Esf.","+6.00 Esf.","+7.00 Esf.","+8.00 Esf.","+9.00 Esf.","+10.00 Esf."],
                ["1.50","1.60","1.67","1.74"],
                ["-10.00 Esf.","-9.00 Esf.","-8.00 Esf.","-7.00 Esf.","-6.00 Esf.","-5.00 Esf.","-4.00 Esf.","-3.00 Esf.","-2.00 Esf.","-1.00 Esf.","Plano","+1.00 Esf.","+2.00 Esf.","+3.00 Esf.","+4.00 Esf.","+5.00 Esf.","+6.00 Esf.","+7.00 Esf.","+8.00 Esf.","+9.00 Esf.","+10.00 Esf."]]

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 4
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return data[component].count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return data[component][row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let picker1 = data[0][pickerView.selectedRowInComponent(0)]
        let picker2 = data[1][pickerView.selectedRowInComponent(1)]
        let picker3 = data[2][pickerView.selectedRowInComponent(2)]
        let picker4 = data[3][pickerView.selectedRowInComponent(3)]
    }
}

Example

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Possible duplicate: [How to change the picker rows based on another picker's selection?](http://stackoverflow.com/q/8924886/2305521) – fpg1503 Jan 09 '16 at 23:45
  • @fpg1503 the linked question it is not Swift – Leo Dabus Jan 09 '16 at 23:50
  • @LeoDabus there was a [discussion on Meta](http://meta.stackoverflow.com/a/303339/2305521) a while ago but I understand that even though it's a question about `UIKit` it may [be beneficial to have a new answer in Swift](http://meta.stackoverflow.com/a/303483/2305521). I tried to write a "language agnostic" answer, though. – fpg1503 Jan 10 '16 at 00:00

1 Answers1

0

You should alter numberOfRowsInComponent e titleForRow forComponent so they take the selection of the third row into account.

After that you can simply call reloadComponent on the last component when the third one is changed.

You can get more info in one of the following SO questions:

Community
  • 1
  • 1
fpg1503
  • 7,492
  • 6
  • 29
  • 49