Reputation: 27
I am having at bit og trouble with my PickerView
. First my ViewController
does not accept UIPickerViewDataSource
no matter what I do. Second, the PickerView will only show the first row. I need it to show both rows and display in one TextField called school
What do I do wrong ?????
Can anyone help me before I go crazy
enter code here
private let typeComponent = 0
private let areaComponent = 1
private let typeOption = ["HighSchool of", "College of", "University of"]
private let areaOption = ["Miami", "Los Angeles", "New York"]
// PickerView - School 2 af 5
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
// PickerView - School 3 af 5
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == typeComponent {
return typeOption.count
} else {
return areaOption.count
}
}
// PickerView - School 4 af 5
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == typeComponent {
return typeOption[row]
} else {
return areaOption[row]
}
}
// PickerView - School 5 af 5
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// school.text = pickOption[component][row]
let typeRow = pickerView.selectedRow(inComponent: (0))
let areaRow = pickerView.selectedRow(inComponent: (1))
let type = typeOption[typeRow]
let area = areaOption[areaRow]
school.text = "\(type)\(area)"
}
override func viewDidLoad() {
super.viewDidLoad()
//Placeholder TextView Interest and about 1 ag 3
Interest.delegate = self
Interest.text = "What are your interests..."
Interest.textColor = UIColor.lightGray
about.delegate = self
about.text = "Tell about yourself..."
about.textColor = UIColor.lightGray
//PickerView School 1 af 5
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
pickerView.backgroundColor = UIColor.white
school.inputView = pickerView
Upvotes: 0
Views: 1387
Reputation: 1713
Make sure your class conforms to UIPickerViewDataSource
protocol like this:
class YourViewController: UIPickerViewDataSource {
//
}
Updated:
For Swift 3 you have to use this datasource function:
func numberOfComponents(in pickerView: UIPickerView) -> Int{
}
and not the one you used.
Upvotes: 0
Reputation: 9226
Implement all the required methods of UIPickerViewDataSource
. If you not conform to UIPickerViewDataSource
it will give you error.
Methods which is Required to implement in your conforming class
in Swift 2.0
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
in Swift 3.0
func numberOfComponents(in pickerView: UIPickerView) -> Int
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
Upvotes: 1