Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing selected index #13

Closed
YSDC opened this issue Jul 18, 2017 · 3 comments
Closed

Missing selected index #13

YSDC opened this issue Jul 18, 2017 · 3 comments
Labels

Comments

@YSDC
Copy link

YSDC commented Jul 18, 2017

Could be interesting to have the index of the selected item.

I've done it locally directly in the McPicker.swift file here:

    public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.pickerSelection[component] = pickerData[component][row]
        self.pickerSelection[1] = String(row)
    }

I then get the result in the done Handler:

            mcPicker.show { (selections: [Int : String]) -> Void in
                self.mySelection = myArray[Int(selections[1]!)!]
            }
@kmcgill88
Copy link
Owner

McPicker isn't meant to give you an index, its meant to give you the selection result. Thats why the doneHandler gives you the selected string per component.

One thing I didn't put in the README which I should perhaps clarify. McPicker takes an array of string arrays, data: [[String]], as a parameter. So, if you passed

let passedInData: [[String]] = [
    ["Sir", "Mr", "Mrs", "Miss"], // Component 0
    ["Kevin", "Lauren", "Kibby", "Stella"] // Component 1
]

and during initialization McPicker by default sets selections to [0 : "Sir", 1 : "Kevin"] for initial values (aka, first item per component). When you select done the doneHandler passes back

mcPicker.show { selections in
        if let prefix = selections[0], let name = selections[1] {
            self.label.text = "\(prefix) \(name)"
        }
}

The label would then read Sir Kevin and from my perspective there is no need to reference back to passedInData as you do in your example with myArray.

If you really do want to know the index I'd suggest doing something like this

// Say you passed this
let passedInData: [[String]] = [
    ["Sir", "Mr", "Mrs", "Miss"], // Will be component 0
    ["Kevin", "Lauren", "Kibby", "Stella"] // Component 1
]

// Imagine a user selected these
let selections: [Int:String] = [0 : "Sir", 1 : "Stella"]

// Inside the doneHandler do
if let secondComponentSelection = selections[1] {
    print(passedInData[1].index(of: secondComponentSelection)!)
    // Prints 3
}  else {
    print("Component selection doesn't exists")
}

// repeat per selection

Hopefully this helps. If not, feel free to make a PR and we can have a look at what you have in mind.

@creative-dev-lab
Copy link

@kmcgill88 You saved my day, Thanks!

@isaranjha
Copy link

isaranjha commented Jan 6, 2021

The provided solution doesn't work if there are repeats in the data array. For example, if you display a McPicker for selecting user named folders, there is no guarantee that the user has named each folder uniquely. Returning the index of the selected item and then cross referencing that to the data source used when setting up the McPicker is the superior and only safe way to accomplish this. What do you think, @kmcgill88?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants