gpt4 book ai didi

ios - 为什么用 UIPickerView 不显示图片?

转载 作者:行者123 更新时间:2023-11-28 07:33:52 27 4
gpt4 key购买 nike

做什么

我希望在用户使用 UIPickerView 时显示图片和消息。现在它可以使用 UIPickerview 显示消息,但是不显示图片。我想用数组,但我不能用。这就是我编写代码并出错的原因。

发生了什么?

我尝试编写用于在选择 UIPickerView 时显示图片的代码。应用程序已完成并显示。但是,没有显示图片。

来源

//ViewController.swift

import UIKit

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
@IBOutlet weak var introduce: UITextView!
@IBOutlet weak var imageV: UIImageView!
@IBOutlet weak var pickerview: UIPickerView!
let names = ["A","B","C","D","E"]
let message = [
"A",
"B",
"C",
"D",
"E"]


//let picture = ["A.png","B.png","C.png","D.png","E.png"]



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Delegate Setting
pickerview.delegate = self
pickerview.dataSource = self

}

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

var rownumber = 0

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

func pickerView1(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
-> Int {
rownumber = row
return rownumber

}


// What UIPickerView shows at first time
func pickerView(_ pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String? {

return names[row]
}


func readimg(rimg:Int) -> UIImageView {
if rownumber == 0 {

let imageV1 = UIImage(named:"A.png")
imageV.image = imageV1

}
if rownumber == 1 {

let imageV1 = UIImage(named:"B.png")
imageV.image = imageV1

}
if rownumber == 2 {

let imageV1 = UIImage(named:"C.png")
imageV.image = imageV1

}
if rownumber == 3 {

let imageV1 = UIImage(named:"D.png")
imageV.image = imageV1

}
if rownumber == 4 {

let imageV1 = UIImage(named:"E.png")
imageV.image = imageV1

}

return imageV

}

// When Row Of UIPickerView is selected
func pickerView(_ pickerView: UIPickerView,
didSelectRow row: Int,
inComponent component: Int) {
introduce.text = message[row]

// If I write it sentence, it has error.
readimg(rimg: Int)
// Error Code (Editor placeholder in source file/Cannot convert value of type 'Int.Type' to expected argument type 'Int')


}



}

我做了什么?

我检查所有错误并解决。

最佳答案

您需要将整数值传递给 readimg(rimg: Int) 方法。因此,将其更改为在 pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 方法中传递行值。

于是就变成了这个样子。

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
introduce.text = message[row]
readimg(rimg: row)
}

除此之外,如果您决定在该函数内分配 imageV 值,readimg 方法不需要返回 UIImageView,因此您可能想要更改其方法签名以返回 void。

func readimg(rimg:Int) {
// Your method implementation
}

关于ios - 为什么用 UIPickerView 不显示图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53824816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com