在UITableViewController中使用UIPickerView



我正在尝试将UIPickerView添加到我的UITableViewController中,但它不允许我输入UIPickerViewDataSource。

我无法为选取器视图设置数据源...那么这将如何工作呢?

我收到此错误:无法将类型"TheTableViewController"的值分配给类型"UIPickerViewDataSource?"的类型。

我已经四处寻找解决方案,但找不到它。谢谢!

我有这个代码

// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
}
// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
}

它工作正常,直到我添加 UIPicker数据源。

您需要

确保符合 UIPickerViewDataSource,并且具有所有必需的方法和完全正确的名称。

因此,您需要更改为这些:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
}

最新更新