1) 완성 이미지
2) 순서
3) 자세히
1. 배열 만들기
var country = ["kota","kita"]
var moment:[String] = []
var momentImage:[String] = []
let kota = ["day1","day2","day3"]
let kita = ["day1","day2"]
let kotaImageNames = ["kota-1.jpg","kota-2.jpg","kota-3.jpg"]
let kitaImageNames = ["kita-1.jpg","kita-2.jpg"]
2. 클래스 추가
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
3. component 개수 설정
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
4. row 개수 설정
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return country.count
} else {
return moment.count
}
}
5. component의 row 지정
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return country[row]
} else {
return moment[row]
}
}
6. component + row 에 내용 + 이미지 지정
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 && row == 0 {
moment = kota
momentImage = kotaImageNames
} else if component == 0 && row == 1 {
moment = kita
momentImage = kitaImageNames
}
7. component reload 설정
pickerView.reloadAllComponents()
8. 이미지뷰에 이미지 나오게 설정
imgView.image = UIImage (named: momentImage[pickerView.selectedRow(inComponent: 1)])
4) 전체 소스
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var imgView: UIImageView!
var country = ["kota","kita"]
var moment:[String] = []
var momentImage:[String] = []
let kota = ["day1","day2","day3"]
let kita = ["day1","day2"]
let kotaImageNames = ["kota-1.jpg","kota-2.jpg","kota-3.jpg"]
let kitaImageNames = ["kita-1.jpg","kita-2.jpg"]
override func viewDidLoad() {
super.viewDidLoad()
moment = kota
momentImage = kotaImageNames
imgView.layer.cornerRadius = 50.0
imgView.layer.masksToBounds = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return country.count
} else {
return moment.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return country[row]
} else {
return moment[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 && row == 0 {
moment = kota
momentImage = kotaImageNames
} else if component == 0 && row == 1 {
moment = kita
momentImage = kitaImageNames
}
pickerView.reloadAllComponents()
imgView.image = UIImage (named: momentImage[pickerView.selectedRow(inComponent: 1)])
}
}