UIPickerView 文字加图片

Posted

技术标签:

【中文标题】UIPickerView 文字加图片【英文标题】:UIPickerView text plus image 【发布时间】:2016-03-09 19:59:03 【问题描述】:

是否可以创建 UIPickerView 将文本和图像排成一行?

我知道如何只用文本创建它:

class FirstViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate 

    var currArray = ["USD","GBP"] 
    var flags = ["USD.jpg", "GBP.jpg"]
    var picker = UIPickerView()

    @IBOutlet weak var currencySelectorLabel: UITextField!

    override func viewDidLoad() 
        super.viewDidLoad()

        picker.delegate = self
        picker.dataSource = self

        currencySelectorLabel.inputView = picker

    


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) 
        self.view.endEditing(true)
    


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


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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        return currArray.count
    

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        currencySelectorLabel.text = currArray[row]
    

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
        return currArray[row]
    

但我想要这样的东西:

显然,而不是图像文件的名称应该是真实的图像。

我想像现在一样从 UIPickerView 分配标签文本。

在这篇帖子 How can I get images to appear in UI PickerView Component in Swift? 的帮助下,一切似乎都正常了!

【问题讨论】:

见***.com/questions/10706459/… @rmaddy 谢谢,但它在 obj-C 中,我不知道如何转换成 Swift2 看答案中使用的方法。这为您指明了正确的方向。 【参考方案1】:

试试这个在 PickerView 中实现标题和图片

@IBOutlet weak var pickerView: UIPickerView!
    
    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        picker.delegate = self
        picker.dataSource = self

        currencySelectorLabel.inputView = picker
    
    
    // MARK: UIPickerViewDataSource
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int 
            return 1
        
        
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
            return currArray.count
        
        
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
            return currArray[row] as? String
        
    // MARK: UIPickerViewDelegate
    
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView 
     
            let myView = UIView(frame: CGRect(x: 0, y: 0, width: pickerView.bounds.width - 50, height: 40))
    
            let myImageView = UIImageView(frame: CGRect(x: 60, y: 0, width: 30, height: 30))
    
            let countryLabel = UILabel(frame: CGRect(x: 110, y: 0, width: pickerView.bounds.width - 90, height: 40 ))
    
            myImageView.image = flags[row]
            countryLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
            countryLabel.textColor = UIColor.white
            countryLabel.text = currArray[row] as? String
            myView.addSubview(countryLabel)
            myView.addSubview(myImageView)
    
            return myView
    
    
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    
        // do something with selected row
    

【讨论】:

以上是关于UIPickerView 文字加图片的主要内容,如果未能解决你的问题,请参考以下文章

点击 uipickerview 时键盘不退出

UIPickerView 最佳实践?

如何使用 Swift 3 UIPickerView 引用多行?

UIPickerView 在重新加载时冻结 UI

在 iPhone 上水平滚动的 UIPickerView?

超过 1 个 UIPickerView Swift