代码之家  ›  专栏  ›  技术社区  ›  Luke Varty

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

  •  0
  • Luke Varty  · 技术社区  · 7 年前

    我试图将1-15加载到选取器视图中,但我的代码返回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 回复  |  直到 7 年前
        1
  •  1
  •   GIJOW    7 年前

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

    尝试使用Xcode自动完成:

    开始键入选择器。。。

    然后会出现一个列表:

    enter image description here

    选择titleForRow方法,按enter键,然后输入您的代码,如:

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

    签名错误:

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