0

I'm trying to add a UIPickerView into my UITableViewController and it doesn't let me enter UIPickerViewDataSource.

I'm not able to set the dataSource for the picker view... so how will this work?

I get this error: Cannot assign value of type 'TheTableViewController' to type 'UIPickerViewDataSource?'

I've looked all over for the solution and unable to find it. Thank you!

I have this code in

// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

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

and it works fine, until I add UIPickerDataSource.

Amit Kalra
  • 4,085
  • 6
  • 28
  • 44
  • Have you set the 'TheTableViewController' to confirm to the UIPickerViewDataSource protocol in it's class definition? – Upholder Of Truth Jan 07 '18 at 11:45
  • Nope, mentioned in my question. It won’t let me. I can only do UITableViewController, UIPickerViewDelegate, and nothing else. It won’t let me add the UIPickerViewDataSource one because UITableViewController won’t allow it. That’s the entire issue :( – Amit Kalra Jan 07 '18 at 11:55
  • Sorry my bad I misunderstood. So I have just setup a test project with this `class ViewController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource {` and then I had to add the functions `func numberOfComponents(in pickerView: UIPickerView) -> Int` and `func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int`. Is that what it won't let you do? – Upholder Of Truth Jan 07 '18 at 11:59
  • No ViewController. Just UITableViewController,UIPicckerViewDelegate, and attempted to do UIPickerViewDataSource and it doesn't work. But yea, the rest is right. It won't let the DataSource stay up there. It says the UITableView does not conform to protocol when I add UIPickerViewDataSource – Amit Kalra Jan 07 '18 at 19:38
  • That's confused me. ViewController is just the name of my class. Can you update the question to show the code you are using. – Upholder Of Truth Jan 07 '18 at 19:39
  • Ohhh! I missed that. Sorry! Yeah, that's correct. Your code is what I'm trying to achieve but I get UITableView does not conform to protocol when I add UIPickerViewDataSource – Amit Kalra Jan 07 '18 at 19:42
  • Have you added the `func numberOfComponents(in pickerView: UIPickerView) -> Int` and `func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int` methods to your UIViewController. If you don't have those methods defined then your controller does not conform to the protocol. Perhaps if you could update the question with your code I could take a look. – Upholder Of Truth Jan 07 '18 at 19:45
  • Updated! And yea, I do have those methods. – Amit Kalra Jan 07 '18 at 19:51
  • If you are using Swift 4 those methods should be `func numberOfComponents(in pickerView: UIPickerView) -> Int` and `func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int`. Note that yours a subtly different. – Upholder Of Truth Jan 07 '18 at 19:54
  • On func numberOfComponents(in pickerView: UIPickerView) -> Int { } how would I return the row? – Amit Kalra Jan 07 '18 at 19:59
  • The other one is also subtly wrong. It's missing a '_' character before the pickerView parameter. – Upholder Of Truth Jan 07 '18 at 20:01
  • Yea, I added yours but how would I return the row on func numberOfComponents(in pickerView: UIPickerView) -> Int { } – Amit Kalra Jan 07 '18 at 20:06
  • I don't understand. The only thing you have incorrect was the name of the two functions. The code in them was ok. Here is the link to the documentation for the UIPickerViewDataSource showing it only has two methods: https://developer.apple.com/documentation/uikit/uipickerviewdatasource – Upholder Of Truth Jan 07 '18 at 20:13
  • I have this. func numberOfComponents(in pickerView: UIPickerView) { } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return pickerData.count } and UIPickerViewDataSource still refuses to work and I can't return rows on the first function so I'm not sure how this is working? – Amit Kalra Jan 07 '18 at 20:23
  • It should be `func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } to get the UIPickerViewDataSource working. It needs to return an Int. You must ensure that the required protocol methods exactly match the definitions that the protocol demands. – Upholder Of Truth Jan 07 '18 at 20:25
  • Boom! It started to work! Thank you! (Add it as an Answer and I'll upvote it) – Amit Kalra Jan 07 '18 at 20:35
  • 1
    No problem glad I could help – Upholder Of Truth Jan 07 '18 at 20:35

1 Answers1

1

You need to make sure you conform to the UIPickerViewDataSource and have all the required methods with the exactly correct names.

So you need to change to these:

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
}
Upholder Of Truth
  • 4,643
  • 2
  • 13
  • 23