?尝试将数据源加载到UIPickerView时返回

问题描述 投票:0回答:1

我试图将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.
}
*/
ios swift xcode uipickerview
1个回答
1
投票

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

尝试使用Xcode自动完成:

开始输入选择器...

然后会出现一个列表:

enter image description here

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

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

签名错误:

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

© www.soinside.com 2019 - 2024. All rights reserved.