Dynamic PickerView with multiple components: Index out of range
When you call reloadAllComponents()
in your didSelectRow
you haven't updated the selected rows yet so it's crashing.
Change the order around and it should be fine.
Reset the value of the 1st component of UIPickerView when component 0 is changed might be of interest here as well.
Note pickerView.reloadAllComponents()
isn't strictly necessary, as pointed out in the post above. You could just reload the ones that need to change.
A suggestion as well: Your code would be easier to read/maintain if you used say an enum to organize your components so instead of if component == 0
you could have if componentType == .country
(or better yet, a switch
), for example.
UIPickerView multi components in swift
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 []
}
}
How to use multiple UIPickerViews in one view controller
You are referencing a non-existent variable named pickerView
. You should update the parameter name to pickerView
to avoid issues.
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == foodTypeUIPickerView {
return self.foodTypePickerData.count
} else {
return self. nutritionPickerData.count
}
}
Make a similar change to the other delegate/data source methods.
Swift Dynamic UIPickerView
You can try
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
let str = myPickerData[row]
if str == "Brown" {
myPickerData2 = ["Undergraduate"]
}
else if str == "Dartmouth" {
myPickerData2 = ["Undergraduate", "MBA"]
}
else {
myPickerData2 = ["Undergraduate", "JD", "MBA"]
}
pickerView.reloadComponent(1)
}
else {
uniSelected = myPickerData[pickerView.selectedRow(inComponent: 0)]
collegeSelected = myPickerData2[pickerView.selectedRow(inComponent: 1)]
}
}
//
This may contain duplicates but you can try it
let mainDic:[String:[String]] = ["Select Your Dream College":[],
"Brown":["Undergraduate"],
"Dartmouth":["Undergraduate", "MBA"],
"Columbia": ["Undergraduate", "JD", "MBA"],
"Harvard",["Undergraduate", "JD", "MBA"]]
Then
myPickerData2 = mainDic[myPickerData[row]]!
pickerView.reloadComponent(1)
And use this in component 0
myPickerData = Array(mainDic.keys)
Make your dataSource is mainDic
and deal with keys as myPickerData
and currently selected key's value as myPickerData2
Edit : In viewDidLoad
myPickerData = Array(mainDic.keys)
myPickerData2 = mainDic["Select Your Dream College"]! // it's default or simply []
How to three sections in UIPickerView in Swift
You are doing it correctly. Typically components are like section in table view and rows are the rows. The didSelect delegate method passes the component and rows selected, so you could find the particular data from your model which was actually selected. Here is a small sample,
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let pickerView = UIPickerView(frame: CGRectMake(0, 0, 320, 300))
view.addSubview(pickerView)
pickerView.delegate = self
pickerView.dataSource = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 3
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return 10
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
if component == 0{
return "Int: \(row)"
}else if component == 1{
return "String: \(row)"
}
return "Other: \(row)"
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
if component == 0{
println("Int component: \(row) selected")
}else if component == 1{
println("String component: \(row) selected")
}else{
println("Other component: \(row) selected")
}
}
}
If you wish to customize the picker view, you could use the following delegates.
– pickerView:attributedTitleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
Multiple UIPickerView in the same UIView
You don't need tags, just use:
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView == pickerView1 {
//pickerView1
} else if pickerView == pickerView2{
//pickerView2
}
also don't forget to set delegate in IB or in code:
pickerView1.delegate = self
Related Topics
Scenekit Ar Game Fps Getting Low and The Device Getting Hot with Use
Swiftui Multiple Animations for Same Element
Implement a Crosshair Kind Behaviour in Realitykit
Swiftui - How to Change The Button's Image on Click
Swift Textview Align with English and Arabic Languages
Could Not Find an Overload for "Init" That Accepts The Supplied Arguments (Swift)
Parse Weird Bug in Swift That Causes Acl Write Permissions to Change to an Objectid
Swift: Benefits of Curry Function
How to Reload Foreach Loop Items After Sheet Is Dismissed
Delete Tableview Cell, and Remove Data from Firebase
Shortest Code to Create an Array of Random Numbers in Swift
How to Programmatically Change The Xdr Display Reference Mode (Aka Preset) on Macos
Pass and Print All Cases in an Enum in Swift
Rlmexception', Reason: 'Attempting to Modify Object Outside of a Write Transaction
How to Implement Applescript Support in a Swift Macos App