我的PickerView
有点问题。首先,无论我做什么,我的ViewController
都不接受UIPickerViewDataSource
。其次,PickerView只显示第一行。我需要它显示两行并显示在一个名为school的文本字段中
我做错什么了?????
在我发疯之前有人能帮我吗
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
最佳答案
执行UIPickerViewDataSource
的所有必需方法。如果您不符合UIPickerViewDataSource
则会给您带来错误。
在一致类中实现所需的方法
在Swift 2.0中
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
在Swift 3.0中
func numberOfComponents(in pickerView: UIPickerView) -> Int
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
关于swift - PickerView-ViewController不符合协议(protocol)UIPickerViewDataSource,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40918551/