Search code examples
arraysswift2uipickerview

Return items at the same location in multiple arrays


I am creating a small reference app which uses a UIPicker to select an organisation and then display info based on the selection. The basic format is:

Picker Goes Here

"Here is the info for (selected organisation)"

Info 1: (Info from Info 1 array here)

Info 2: (Info from Info 2 array here)

Info 3: (Info from Info 3 array here)

Info 4: (Info from Info 4 array here)

Info 5: (Info from Info 5 array here)

I have completed coding for the picker and it is returning the value of the selected organisation from the array that feeds it.

The other data is contained in separate arrays but the location of the info will be consistent across them all. Organisation at position 0 will have all its details at position 0 in the other arrays.

What I want to do is to return the values from the other arrays which are at the same location as the result returned by the picker. This way, each time the picker is moved, the data will update on the screen.

I cannot find away of referencing the position of the result my picker is returning and using it to access the other info I need to display.

I would welcome other suggestions about how to manage this data if there are any...

Any ideas?


Solution

  • You need to define a delegate for your UIPickerView.

    class Controller:UIViewController, UIPickerViewDelegate {
    
        func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
            print("User selected row \(row)")
        }
    }
    

    Don't forget to wire the delegate property of the Picker view to your ViewController in Interface Builder.

    Now every time the user select a value, the method above will be called. You just need to use the row param to access your arrays.