0

I am trying to get images to show up in the pickerview instead of text. I am seeing ? and no images. I know the images are correct as I am using them in another view.

I tried to follow some other stack overflow posts but to no avail. like this one: How can I get images to appear in UI PickerView Component in Swift?

enter image description here

code:

import UIKit

class ShuffleViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var label: UILabel!

@IBOutlet weak var shufflePicker: UIPickerView!
var pickerData: [String] = [String]()

let images = [UIImage(named:"Acceptance"),
              UIImage(named: "loveimage"),
              UIImage(named: "gratimage"),
              UIImage(named: "successsmall"),
              UIImage(named: "pos"),
              UIImage(named: "rel"),
              UIImage(named: "weal"),
              UIImage(named: "heal2"),
              UIImage(named: "confiden")]

override func viewDidLoad() {
    super.viewDidLoad()

    shufflePicker.delegate = self
    shufflePicker.dataSource = self

}

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


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

}


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


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

return images[row]
}

I'm no sure what to do as I am fairly new to Xcode and swift

steller
  • 245
  • 3
  • 14
  • 2
    You can't change the signature of data source and delegate methods. `titleForRow` returns `String`, not `UIImage?`. And please look at the answer to the question you linked. The answer does not use `titleForRow`. – rmaddy Dec 01 '18 at 18:06

1 Answers1

2

Just you can use this Method

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {


        let myImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 220, height: 61))


        myImageView.image = pickerDataSource[row]


        return myImageView

    }
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35