PickerView shows only '?'

谁说胖子不能爱 提交于 2020-01-17 04:43:05

问题


This is my source code and what I have done is make picker view on storyBoard. Make IBOutlet in this controller by contorl+drag.

It can be compiled however, only '?' appears in the picker view.

Where is the problem?

import UIKit

class SelectViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
    var songNames = ["test1","test2","test3"]

    @IBOutlet weak var songPicker: UIPickerView!

    override func viewDidLoad(){ 
        songPicker.delegate = self
        songPicker.dataSource = self

    }

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

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

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

回答1:


You have missed the forComponent parameter from the dataSource method. Add it in your titleForRow function like this:

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

This should fix the problem you are having.



来源:https://stackoverflow.com/questions/44051485/pickerview-shows-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!