将图像添加到我的 UIPickerView 哪里出错了?

Posted

技术标签:

【中文标题】将图像添加到我的 UIPickerView 哪里出错了?【英文标题】:Where am I going wrong adding images to my UIPickerView? 【发布时间】:2020-03-23 15:23:19 【问题描述】:

我的是一个教育应用程序,给学生一个单词,然后应该选择相应的图像(比如将香蕉这个词与一个图像匹配,其中图像在 UIPickerview 中)但是,我的以下代码产生没有结果:

let possibleAnswers = [UIImage(named: Images.one), UIImage(named: Images.two), UIImage(named: Images.three), UIImage(named: Images.four)]

    fileprivate let pickerView: UIPickerView = 
       let pv = UIPickerView()
        pv.translatesAutoresizingMaskIntoConstraints = false
        return pv
    ()

 override func viewDidLoad() 
    pickerView.delegate = self
    pickerView.dataSource = self
  

extension ViewController: UIPickerViewDataSource 

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

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


extension ViewController: UIPickerViewDelegate 

    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 = possibleAnswers[row]

        return myImageView
    


我做错了吗?我该如何纠正这个问题?

编辑1:

这就是错误观点的样子。

【问题讨论】:

你设置了 UIPickerView 委托吗? @DilanAnuruddha,感谢您的回复。是的,我已正确设置,编辑了问题以更清楚地反映问题 使用rowHeightForComponent函数设置pickerview行高。看我的回答 【参考方案1】:

使用 rowHeightForComponent 函数设置 PickerView 行高以匹配您的图像大小

 class ViewController: UIViewController,UITextFieldDelegate
        fileprivate let possibleAnswers:[UIImage] = [UIImage(named: "apple.png")!,UIImage(named: "banana.png")!,UIImage(named: "orange.png")!]
        fileprivate var pickerView: UIPickerView?
        fileprivate var toolBar:UIToolbar?

        override func viewDidLoad() 
            super.viewDidLoad()

            view.backgroundColor = .systemBackground

            view.addSubview(btn)
            btn.translatesAutoresizingMaskIntoConstraints = false
            btn.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
            btn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            btn.addTarget(self, action: #selector(test), for: .touchUpInside)
        

        //MARK: Components
        let btn :UIButton = 
            let btn = UIButton(type: .system)
            btn.setTitle("Select Correct Answer", for: .normal)
            return btn
        ()

    

    // MARK: Pickerview Delegate
    extension ViewController: UIPickerViewDelegate,UIPickerViewDataSource 
        func numberOfComponents(in pickerView: UIPickerView) -> Int 
               return 1
           

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

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

            let myImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            myImageView.image = possibleAnswers[row]

            return myImageView
        

        func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat 
            return 100
        

    

    //MARK: Functions
    extension ViewController
        @objc func test()
            pickerView = UIPickerView.init()
            pickerView!.delegate = self
            pickerView!.backgroundColor = UIColor.white
            pickerView!.setValue(UIColor.black, forKey: "textColor")
            pickerView!.autoresizingMask = .flexibleWidth
            pickerView!.contentMode = .center
            pickerView!.frame = CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 300)
            self.view.addSubview(pickerView!)

            toolBar = UIToolbar.init(frame: CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 50))
            toolBar!.barStyle = .blackTranslucent
            toolBar!.items = [UIBarButtonItem.init(title: "Done", style: .done, target: self, action: nil)]
            self.view.addSubview(toolBar!)
        
    

【讨论】:

后续问题,您不必回答 :) 如果您必须在用户选择特定图像后按下“选择正确答案按钮”时打印图像名称,您会在哪里打电话didSelectRow 函数? 在我的示例中,您在用户选择图像toolBar!.items = [UIBarButtonItem.init(title: "Done", style: .done, target: self, action: #selctor(didUserSelectImage))]后调用了一些函数

以上是关于将图像添加到我的 UIPickerView 哪里出错了?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 PHAsset 中添加图像并将其从照片库中删除?

可以将下载的图像/文件添加到我的应用程序包中吗?

如何将可点击的图像添加到 UIPickerView 行?

我应该如何以及在哪里将 Glyphicons 文件夹添加到我的项目中?

如何将子视图添加到我的 stackView?我试试看,但我看不到它们

为啥每当我尝试将图像添加到我的 django 项目时都会出错?