?尝试将数据源加载到UIPickerView

时间:2018-03-06 12:43:18

标签: ios swift xcode uipickerview

我正在尝试将1 - 15加载到Picker View中,但是使用我的代码,它会返回15个问号。我在下面的代码中做错了什么?我已经按照它的指南,但这似乎不起作用。它必须是一个小的东西,我必须调整,使这项工作。

class NewDaySplitViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var picker: UIPickerView!

var pickerData: [String] = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.picker.delegate = self
    self.picker.dataSource = self

    pickerData = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

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
private func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

1 个答案:

答案 0 :(得分:1)

您的委托方法titleForRow的签名不正确。

尝试使用Xcode自动完成:

开始输入选择器......

然后会出现一个列表:

enter image description here

选择titleForRow方法,按回车键并输入如下代码:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
}

签名错误:

该方法不是私密的,您没有_作为第一个参数。