我希望在用户使用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')
}
}
我检查所有错误并解决。
答案 0 :(得分:0)
您需要将整数值传递给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)
}
除此之外,如果您决定在该函数内分配readimg
值,则UIImageView
方法不需要返回imageV
,因此您可能需要更改其方法签名以返回无效。
func readimg(rimg:Int) {
// Your method implementation
}