问题
I found this answer How do I setup a second component with a UIPickerView and it was helpful but I want to do more. I want to have the data of the second component dependent on the first. here is my code
class timerScreen: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
// where all the outlets are setup
@IBOutlet weak var pickerViewOutlet: UIPickerView!
// where I try to set up all my variables and constants
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"]
let firstArray = ["cross", "OLL", "Pll", "Corners", "Edges"]
let SecondArray = ["OLL" "Pll"]
// where the picker view is set up.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return cubesToWorkWith.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return cubesToWorkWith[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
cubeSelected = Int16(row)
}
}
I want the firstArray to be displayed in the second component if the "3X3" is currently selected in the first component and the secondArray if the "2X2" is selected. How would you do that? thanks for any help in advance!!!
回答1:
Try do like this. I dont know what you exactly want. But it seems you want to reload the components on row selection of first component. If this is the requirement then this code will work.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//row = [repeatPickerView selectedRowInComponent:0];
var row = pickerView.selectedRowInComponent(0)
println("this is the pickerView\(row)")
if component == 0 {
return cubesToWorkWith.count
}
else {
return firstArray.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if component == 0 {
return cubesToWorkWith[row]
} else {
return getArrayForRow(row)[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
pickerView.reloadComponent(1)
}
}
func getArrayForRow(row: Int) -> [String] {
switch row { // check if 2*2 0r 3*3
case 0:
return firstArray
case 1:
return secondArray
// and so on...
default:
return []
}
}
回答2:
You can use simple if
/else
or switch
cases to pick the right array:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return cubesToWorkWith[row]
} else {
if cubesToWorkWith[lastSelectedCube] == "3X3" {
return firstArray[row]
} else if cubesToWorkWith[lastSelectedCube] == "2X2" {
return secondArray[row]
} else /* You did not mention what to show for other selections, that would be handled here */ {
return "undefined"
}
}
}
You can copy the same to numberOfRowsInComponent
and return the count
of the arrays.
Complete code:
class timerScreen: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
// where all the outlets are setup
@IBOutlet weak var pickerViewOutlet: UIPickerView!
// where I try to set up all my variables and constants
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"]
let firstArray = ["cross", "OLL", "Pll", "Corners", "Edges"]
let secondArray = ["OLL", "Pll"]
var lastSelectedCube = 0
// where the picker view is set up.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return cubesToWorkWith.count
} else {
if cubesToWorkWith[lastSelectedCube] == "3X3" {
return firstArray.count
} else if cubesToWorkWith[lastSelectedCube] == "2X2" {
return secondArray.count
} else /* You did not mention what to show for other selections, that would be handled here */ {
return 0
}
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return cubesToWorkWith[row]
} else {
if cubesToWorkWith[lastSelectedCube] == "3X3" {
return firstArray[row]
} else if cubesToWorkWith[lastSelectedCube] == "2X2" {
return secondArray[row]
} else /* You did not mention what to show for other selections, that would be handled here */ {
return "undefined"
}
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
lastSelectedCube = row
pickerView.reloadComponent(1)
}
}
}
As both if clauses look very similar, you can now refactor them out in an extra method:
...
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let contentArray = content(for: component)
return contentArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let contentArray = content(for: component)
if row >= contentArray.count {
return nil
}
return contentArray[row]
}
func content(for component: Int) -> [String] {
if component == 0 {
return cubesToWorkWith
} else {
if cubesToWorkWith[lastSelectedCube] == "3X3" {
return firstArray
} else if cubesToWorkWith[lastSelectedCube] == "2X2" {
return secondArray
} else /* You did not mention what to show for other selections, that would be handled here */ {
return []
}
}
}
...
来源:https://stackoverflow.com/questions/42316756/uipickerview-multi-components-in-swift