如何在Swift中的UI PickerView组件中显示图像?

时间:2015-01-04 19:04:28

标签: ios swift uipickerview

我正在尝试在Swift PickerView中使用图像。我不知道如何让图像实际出现在组件中。我知道如何使用带有titleForRow函数的字符串来执行此操作,但我不知道如何使用图像执行此操作。到目前为止,这是我的代码:

import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {

    @IBOutlet weak var pickerView: UIPickerView!


    var imageArray: [UIImage] = [UIImage(named: "washington.jpg")!,
        UIImage(named: "berlin.jpg")!, UIImage(named: "beijing.jpg")!,
        UIImage(named: "tokyo.jpg")!]



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // returns the number of 'columns' to display.
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{

        return 1

    }

    // returns the # of rows in each component..
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        return imageArray.count
    }



}// end of app

5 个答案:

答案 0 :(得分:17)

您需要为UIPickerViewDelegate协议实现更多的委托方法。特别是一个rowHeight委托方法和一个viewForRow委托方法。

类似的东西:

// MARK: UIPickerViewDataSource

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

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

 func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 60
}


// MARK: UIPickerViewDelegate

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {

    var myView = UIView(frame: CGRectMake(0, 0, pickerView.bounds.width - 30, 60))

    var myImageView = UIImageView(frame: CGRectMake(0, 0, 50, 50))

    var rowString = String()
    switch row {
    case 0:
        rowString = “Washington”
        myImageView.image = UIImage(named:"washington.jpg")
    case 1:
        rowString = “Beijing”
        myImageView.image = UIImage(named:"beijing.jpg")
    case 2:
        default:
        rowString = "Error: too many rows"
        myImageView.image = nil
    }
    let myLabel = UILabel(frame: CGRectMake(60, 0, pickerView.bounds.width - 90, 60 ))
    myLabel.font = UIFont(name:some font, size: 18)
    myLabel.text = rowString

    myView.addSubview(myLabel)
    myView.addSubview(myImageView)

    return myView
}

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

   // do something with selected row
}

请注意,标签布局等仅用于演示,需要调整,或者更好地使用自动布局等。

答案 1 :(得分:2)

我有同样的问题并做了一些研究,直到我弄清楚了。这是一个对我很有用的例子。只需确保Assets文件夹中的图像都与您的案例字符串的名称相同,您就可以设置了!

@IBOutlet weak var pickerView: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    pickerView.delegate = self
}

// MARK: UIPickerViewDataSource

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

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

// MARK: UIPickerViewDelegate

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


    var myImageView = UIImageView()

    switch row {
    case 0:
        myImageView = UIImageView(image: UIImage(named:"airplane"))
    case 1:
        myImageView = UIImageView(image: UIImage(named:"beach"))
    case 2:
        myImageView = UIImageView(image: UIImage(named:"bike"))
    case 3:
        myImageView = UIImageView(image: UIImage(named:"hiking"))
    case 4:
        myImageView = UIImageView(image: UIImage(named:"ironman"))
    case 5:
        myImageView = UIImageView(image: UIImage(named:"moneybag"))
    case 6:
        myImageView = UIImageView(image: UIImage(named:"moneybills"))
    case 7:
        myImageView = UIImageView(image: UIImage(named:"ninjaturtle"))
    case 8:
        myImageView = UIImageView(image: UIImage(named:"running"))
    case 9:
        myImageView = UIImageView(image: UIImage(named:"shoppingcart"))
    case 10:
        myImageView = UIImageView(image: UIImage(named:"workingout"))
    default:
        myImageView.image = nil

        return myImageView
    }
    return myImageView
}

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

    // do something with selected row
}

答案 2 :(得分:2)

在处理很多事情时,制作开关语句可能会很乏味。

相反,请使用像这样的for循环

for _ in 1..<imageArray.count {
myImageView.image = UIImage(named: imageArray[row])

}

答案 3 :(得分:0)

您不需要子类UIPickerView(事实上,无论如何,这可能不是明智的子类视图)。相反,您需要有一个实现UIPickerViewDelegate协议的对象。在此对象中,您将实现方法

optional func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView

此方法提供单个组件的视图。在该方法中,您将从图像数组中返回适当的值。

答案 4 :(得分:0)

isCompatible