在 UITableViewController 中使用 UIPickerView

Using UIPickerView inside UITableViewController

我正在尝试将 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]
}

它工作正常,直到我添加 UIPickerDataSource。

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

所以你需要改成这些:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

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