2

I have error with using UIPickerView

SelectViewController.swift:35:10: Method 'pickerView(pickerView:numberOfRowsInComponent:)' has different argument names from those required by protocol 'UIPickerViewDataSource' ('pickerView(_:numberOfRowsInComponent:)')

I set UIPickerView on Storyboard and attached this to the songPicker variable.

and then I think I integrated the functions necessary though, it showed error like this.

I found out that structure of picker view is changed on the version of swift.

However can't find the correct answer yet.

My swift is 3.1

Does anyone help me?

class SelectViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

    @IBOutlet weak var songPicker: UIPickerView!

    let songNames = ["test","test2"]

    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]
    }
    override func didReceiveMemoryWarning() {

    }
}
Dilip Tiwari
  • 1,441
  • 18
  • 31
whitebear
  • 11,200
  • 24
  • 114
  • 237
  • Remove this func .. func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1} – Hobbit May 18 '17 at 08:11

4 Answers4

2

try this code

class ElibraryViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource {

@IBOutlet weak var txtTitle: UITextField!
 @IBOutlet weak var Picker: UIPickerView!
let songNames = ["test","test2"]

override func viewDidLoad()
    {

        super.viewDidLoad()


            }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {


            return songNames[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {



        let obj = songNames[row]
        txtTitle.text = obj



    }

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

        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {



        return songNames.count



    }
override func didReceiveMemoryWarning() {

    }
}
Amul4608
  • 1,390
  • 14
  • 30
  • there is no meaning in repeating answers?, you already seen there is same answer available, then why to put the same thing again?. Make the stackoverflow a better place. – Mehul Thakkar May 18 '17 at 09:19
1

Use this method, see the use of _ before pickerview. That is the only problem

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    }
Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
  • Thanks it works! I need to learn Swift more. BTW,my pickerviews show only '?' in the field instead of "test1" "test2" this might be another problem though.... – whitebear May 18 '17 at 13:55
  • I think there may be some other problem. Need your full code to check, otherwise, the code that you provided should show test1 and test2 – Mehul Thakkar May 19 '17 at 04:46
1

UIPickerViewDataSource is changes in swift 3 and above.

Updated syntax:-

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

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

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

}

Have a look at apple documentation (SO reference).

Community
  • 1
  • 1
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • there is no meaning in repeating answers?, you already seen there is same answer available, then why to put the same thing again?. Make the stackoverflow a better place. – Mehul Thakkar May 18 '17 at 09:19
0

I recommend you to use IQDropDownTextField - Click Here

Ved Rauniyar
  • 1,539
  • 14
  • 21