iOS

[ IOS ] ํ”ผ์ปค ๋ทฐ ๋งŒ๋“ค๊ธฐ ( ํ”„๋กœํ† ์ฝœ, ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ )

Forest Yun 2021. 7. 25. 22:32
728x90

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

 

๋ฌธ์ œ

์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‘ ๊ฐœ์ธ ํ”ผ์ปค ๋ทฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. ์™ผ์ชฝ ํ”ผ์ปค ๋ทฐ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ ํŒŒ์ผ ๋ช…์ด ๋‚˜ํƒ€๋‚˜๋„๋ก, ์˜ค๋ฅธ์ชฝ ํ”ผ์ปค ๋ทฐ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ ์ด๋ฏธ์ง€ ๋ทฐ์— ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ•œ๋‹ค.

 

 

๊ตฌํ˜„ ์ˆœ์„œ

  1. ๋ ˆ์ด์•„์›ƒ ์ž‘์„ฑํ•˜๊ธฐ
  2. ์•„์šธ๋ › ๋ณ€์ˆ˜ ์ƒ์„ฑํ•˜๊ธฐ
  3. ํ”ผ์ปค ๋ทฐ์˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์„ค์ •ํ•˜๊ธฐ
  4. ํ•„์š”ํ•œ ํด๋ž˜์Šค ์ƒ์†๋ฐ›๊ธฐ(ํ”„๋กœํ† ์ฝœ ์ฑ„ํƒ), ๋ณ€์ˆ˜ ๋ฐ ์ƒ์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ, ์ดˆ๊ธฐํ™”๋ฉด ์„ค์ •ํ•˜๊ธฐ
  5. ํ”ผ์ปค ๋ทฐ ๋™์ž‘ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
  6. ์ฒซ ๋ฒˆ์งธ ํ”ผ์ปค ๋ทฐ ์„ ํƒ ์‹œ ํŒŒ์ผ ๋ช… ์ถœ๋ ฅํ•˜๊ธฐ
  7. ๋‘ ๋ฒˆ์งธ ํ”ผ์ปค ๋ทฐ ์„ ํƒ์‹œ ์ด๋ฏธ์ง€ ์ถœ๋ ฅํ•˜๊ธฐ
  8. ํ”ผ์ปค ๋ทฐ ๋ฃฐ๋ › ํ–‰ ๋†’์ด ์„ค์ •

 

์ตœ์ข… ๊ฒฐ๊ณผ

 

 

 

 

 


โ€ป ๊ฐœ๋… ์‚ดํŽด๋ณด๊ธฐ

 

ํ”„๋กœํ† ์ฝœ Protocol

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋Š” ํ”„๋กœํ† ์ฝœ๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ”„๋กœํ† ์ฝœ์„ ๋จผ์ € ์•Œ์•„๋ณด์ž.

Protocol

(1) ์™ธ๊ต ์˜๋ก€, ์˜์ „
(2) (์กฐ์•ฝ์˜) ์ดˆ์•ˆ, ๋ณด์ถฉํ˜‘์•ฝ
(3) ์ปดํ“จํ„ฐ ํ†ต์‹  ๊ทœ์•ฝ
(4) (๊ณผํ•™ ์‹คํ—˜, ์˜๋ฃŒ ์น˜๋ฃŒ์˜) ๊ณ„ํš์„œ
  • ํŠน์ • ์—ญํ• ์„ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ, ํ”„๋กœํผํ‹ฐ, ๊ธฐํƒ€ ์š”๊ตฌ์‚ฌํ•ญ ๋“ฑ์˜ ์ฒญ์‚ฌ์ง„์„ ์ •์˜ํ•œ๋‹ค.
  • ํ”„๋กœํ† ์ฝœ์€ ์ •์˜๋ฅผ ํ•˜๊ณ  ์ œ์‹œ๋ฅผ ํ•  ๋ฟ ์Šค์Šค๋กœ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ธฐ๋Šฅ์˜ ๊ตฌํ˜„์€ ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒ(Adopted)ํ•œ ๊ณณ์—์„œ ์ด๋ฃจ์–ด์ง„๋‹ค.
  • ํ”„๋กœํ† ์ฝœ์€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋„์šธ ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ์ด ์ •์˜๋˜์–ด์žˆ๋‹ค. ์ฆ‰ ์šฐ๋ฆฌ๋Š” ์ฑ„ํƒํ•œ ํ”„๋กœํ† ์ฝœ์—์„œ ์›ํ•˜๋Š” ๊ธฐ๋Šฅ๋งŒ์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ž๋ฐ”์˜ interface์™€ ์œ ์‚ฌํ•˜๋‹ค.

 

 

 

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ Delegate

delegate
1. (์ง‘๋‹จ์˜ ์˜์‚ฌ๋ฅผ ๋Œ€ํ‘œํ•˜๋Š”) ๋Œ€ํ‘œ(์ž)
2. (๊ถŒํ•œ, ์—…๋ฌด ๋“ฑ์„) ์œ„์ž„ํ•˜๋‹ค
3. (๋Œ€ํ‘œ๋ฅผ) ๋ฝ‘๋‹ค
  • ๋Œ€๋ฆฌ์ž. ๋ˆ„๊ตฐ๊ฐ€ ํ•ด์•ผ ํ•  ์ผ์„ ๋Œ€์‹ ํ•ด์ฃผ๋Š” ์—ญํ• 
  • delegate๋Š” ํ”„๋กœํ† ์ฝœ๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ๋‹ค.
  • '์œ„์ž„ํ•˜๋‹ค'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„ ๊ฒƒ์ฒ˜๋Ÿผ, A๊ฐ€ ํ•ด์•ผํ•  โ˜…์„, B๊ฐ€ ๊ถŒํ•œ์„ ์œ„์ž„๋ฐ›์•„ โ˜…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • ํด๋ž˜์Šค ์„ ์–ธ๋ฌธ ์˜ค๋ฅธ์ชฝ์— ์ฑ„ํƒํ•  ํ”„๋กœํ† ์ฝœ์„ ์ ๋Š”๋‹ค. (์ƒ์†๋ฐ›์„ ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ์ ์€ ํ›„ ์ฑ„ํƒํ•  ํ”„๋กœํ† ์ฝœ์„ ๋‚˜์—ดํ•œ๋‹ค.)
class ํด๋ž˜์Šค๋ช…: ์ƒ์†๋ฐ›์„ ํด๋ž˜์Šค๋ช…, ์ฑ„ํƒํ•  ํ”„๋กœํ† ์ฝœ {
	...
}
  • ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ์ฒด๋ฅผ ํ„ฐ์น˜ํ–ˆ์„ ๋•Œ ํ•ด์•ผ ํ•  ์ผ์„ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ์— ๊ตฌํ˜„ํ•˜๊ณ , ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ํ„ฐ์น˜๋˜์—ˆ์„ ๋•Œ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ์œ„์ž„๋ฐ›์€ ์ผ์„ ํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์•ก์…˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜์—ฌ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋กœํ† ์ฝœ์— ์ •์˜๋˜์–ด์žˆ๋Š” ๋งŽ๊ณ  ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ์œ ์šฉํ•˜๋‹ค.

 




 

 

 

 

 

 

1. ๋ ˆ์ด์•„์›ƒ ์ž‘์„ฑํ•˜๊ธฐ

 

 

 

 

 


2. ์•„์šธ๋ › ๋ณ€์ˆ˜ ์ƒ์„ฑํ•˜๊ธฐ

import UIKit

class PickerViewMissionController: UIViewController {
                                   
    @IBOutlet var pickerImage: UIPickerView!
    @IBOutlet var lblImageFileName: UILabel!
    @IBOutlet var imageView: UIImageView!
}

 

 

 

 

 


3. ํ”ผ์ปค ๋ทฐ์˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์„ค์ •ํ•˜๊ธฐ

๋งˆ์šฐ์Šค์˜ ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์œผ๋กœ ํ”ผ์ปค ๋ทฐ๋ฅผ ์„ ํƒํ•œ ํ›„ ์œ„์ชฝ์˜ ๋ทฐ ์ปจ๋“œ๋กค๋Ÿฌ ์•„์ด์ฝ˜ ์œ„๋กœ ๋Œ์–ด๋‹ค ๋†“๋Š”๋‹ค. 

๊ทธ ํ›„ delegate๋ฅผ ํด๋ฆญํ•˜์—ฌ ํ”ผ์ปค ๋ทฐ์˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

 

 

 

 

 

 


4. ํ•„์š”ํ•œ ํด๋ž˜์Šค ์ƒ์†๋ฐ›๊ธฐ(ํ”„๋กœํ† ์ฝœ ์ฑ„ํƒ), ๋ณ€์ˆ˜ ๋ฐ ์ƒ์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ, ์ดˆ๊ธฐํ™”๋ฉด ์„ค์ •ํ•˜๊ธฐ

 

4.1 UIPickerViewDelegate, UIPickerViewDataSource ํ”„๋กœํ† ์ฝœ ์ฑ„ํƒํ•˜๊ธฐ

import UIKit

class PickerViewMissionController: UIViewController,
                                   UIPickerViewDelegate,
                                   UIPickerViewDataSource {
    ...
}

 

UIPickerViewDelegate

The delegate of a UIPickerView object must adopt this protocol and implement at least some of its methods to provide the picker view with the data it needs to construct itself.

The delegate implements the required methods of this protocol /to return height, width, row title, and the view content for the rows in each component. It must also provide/ the content for each component’s row, /either as a string or a view. Typically the delegate implements /other optional methods/ to respond to new selections or deselections of component rows.

UIPickerView ๊ฐ์ฒด์˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋Š” UIPickerViewDelegate ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•ด์•ผํ•œ๋‹ค, ๊ทธ๋ฆฌ๊ณ  ํ”ผ์ปค ๋ทฐ ๊ตฌ์„ฑ์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–์ถ”๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐœ์˜ ํ•„์ˆ˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

 

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋Š” ๋†’์ด, ๋„ˆ๋น„, ํ–‰(์š”์†Œ) ํƒ€์ดํ‹€ ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์ปดํฌ๋„ŒํŠธ์˜ ํ–‰(์š”์†Œ)์„ ์œ„ํ•œ ๋ทฐ ์ฝ˜ํ…์ธ ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด UIPickerViewDelegate ํ”„๋กœํ† ์ฝœ์ด ํ•„์š”๋กœ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋Š” ๋ฌธ์ž์—ด ๋˜๋Š” ๋ทฐ๋ฅผ ์ด์šฉํ•ด ๊ฐ ์ปดํฌ๋„ŒํŠธ ํ–‰(์š”์†Œ)์— ์ฝ˜ํ…์ธ ๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ ํ–‰(์š”์†Œ)์˜ ์ƒˆ๋กœ์šด ์„ ํƒ ๋˜๋Š” ์„ ํƒ ์ทจ์†Œ์— ์‘๋‹ตํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐํƒ€ ์„ ํƒ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

 

 

UIPickerViewDataSource

The UIPickerViewDataSource protocol must be adopted by an object that mediates between a UIPickerView object and your application’s data model for that picker view. The data source provides the picker view with the number of components, and the number of rows in each component, for displaying the picker view data. Both methods in this protocol are required.

UIPickerView ๊ฐ์ฒด์™€ pickerView๋ฅผ ์œ„ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ ์‚ฌ์ด์— ์ค‘์žฌํ•˜๋Š” ๊ฐ์ฒด๋Š” UIPickerViewDelegate ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•ด์•ผ ํ•œ๋‹ค. 

๋ฐ์ดํ„ฐ ์†Œ์Šค๋Š” pickerView์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด pickerView์— ์ปดํฌ๋„ŒํŠธ ์ˆ˜, ๊ฐ ์ปดํฌ๋„ŒํŠธ์˜ ํ–‰(์š”์†Œ)์˜ ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด ํ”„๋กœํ† ์ฝœ์—์„œ ๋‘ ๋ฉ”์†Œ๋“œ๋Š” ํ•„์ˆ˜์ด๋‹ค.

 

 

 


 

 

 

4.2 ๋ณ€์ˆ˜ ๋ฐ ์ƒ์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ, ์ดˆ๊ธฐํ™”๋ฉด ์„ค์ •ํ•˜๊ธฐ

import UIKit

class PickerViewMissionController: UIViewController,
                                   UIPickerViewDelegate,
                                   UIPickerViewDataSource {
    let MAX_ARRAY_NUM = 3
    let PICKER_VIEW_COLUMN = 2 //ํ”ผ์ปค ๋ทฐ component 2๊ฐœ ํ•„์š”
    var imageArray: Array<UIImage?> = [UIImage?]() //์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
    var imageFileName = ["putArmsAround.jpg",
                         "playingMusic.jpg",
                         "face.png"]               //์ด๋ฏธ์ง€ ํŒŒ์ผ ์ด๋ฆ„์„ ์ €์žฅํ•  ๋ฐฐ์—ด

    @IBOutlet var pickerImage: UIPickerView!
    @IBOutlet var lblImageFileName: UILabel!
    @IBOutlet var imageView: UIImageView!
    
    //๋ทฐ ๋กœ๋“œ์‹œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //UIImage ํƒ€์ž…์˜ imageArray์— ์š”์†Œ ์ถ”๊ฐ€
        for i in 0 ..< MAX_ARRAY_NUM {
            let image = UIImage(named: "WallaceAndGromit_\(imageFileName[i])")
            imageArray.append(image)
        }
        
        //์ดˆ๊ธฐ ํ™”๋ฉด ์„ค์ •
        lblImageFileName.text = imageFileName[0]
        imageView.image = imageArray[0]
        
        }
     
}

 

 

 

 

 

 

 

 

 


5. ํ”ผ์ปค ๋ทฐ ๋™์ž‘์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ

+ component๋Š” ์ธ๋ฐ์Šค 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. ์ธ์ˆ˜์ธ component๋ฅผ ์‚ฌ์šฉํ•ด component๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • numberOfComponent : ํ”ผ์ปค ๋ทฐ์— ํ‘œ์‹œ๋˜๋Š” ์—ด ๊ฐœ์ˆ˜, ์ฆ‰ ํ”ผ์ปค ๋ทฐ ์ปดํฌ๋„ŒํŠธ์˜ ์ˆ˜๋ฅผ ํ”ผ์ปค ๋ทฐ์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
    //returns the number of components(columns) to display
    //ํ™”๋ฉด์— ๋ณด์—ฌ์ค„ component ๊ฐœ์ˆ˜(์—ด)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(ํ”ผ์ปค ๋ทฐ์˜ component ๊ฐœ์ˆ˜(์—ด)๋ฅผ ์ •ํ•ด์ฃผ๋Š” ๊ณณ)
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return PICKER_VIEW_COLUMN
    }

 

  • numberOfRowsInComponent ์ธ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” pickerView : ๊ฐ ์ปดํฌ๋„ŒํŠธ์˜ ํ–‰(์š”์†Œ)์˜ ๊ฐœ์ˆ˜๋ฅผ ํ”ผ์ปค ๋ทฐ์— ์ „๋‹ฌํ•˜๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
	//Returns The number of rows for the component.
    //numberOfRowsInComponent์ธ์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
    //ํ•œ component์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    //component๋Š” ์ธ๋ฐ์Šค 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. ์ธ์ˆ˜์ธ component๋ฅผ ์‚ฌ์šฉํ•ด component๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return imageFileName.count
    }

 

  • titleForRow ์ธ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” pickerView :  ๊ฐ ์ปดํฌ๋„ŒํŠธ์˜ ํ–‰ ํƒ€์ดํ‹€์„ ํ”ผ์ปค ๋ทฐ์— ๋ฌธ์ž์—ด ๊ฐ’์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
//์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๋ฉ”์„œ๋“œ

	func pickerView(_ pickerView: UIPickerView, 
    				titleForRow row: Int, forComponent componenet) -> String? {
         return imageFileName[row]  //์–ด๋–ค ํ–‰(์š”์†Œ)์— ํ•ด๋‹นํ•˜๋Š” ๋ฐฐ์—ด ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 
    }

 

  • viewForRow ์ธ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” pickerView : ๊ฐ ์ปดํฌ๋„ŒํŠธ์˜ ํ–‰(์š”์†Œ)์— ๋œฐ view๋ฅผ ํ”ผ์ปค ๋ทฐ์— UIView ๊ฐ’์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
    //viewForRow ์ธ์ˆ˜๊ฐ€ ์žˆ๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ๋Š” component์— UIView๊ฐ€ ๋œจ๋„๋ก ํ•œ๋‹ค. -  ์ด๋ฏธ์ง€๋ฅผ ๋œจ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int,forComponent component: Int, reusing view: UIView?) -> UIView {
        let imageView = UIImageView(image: imageArray[row])
        imageView.frame = CGRect(x: 0, y : 0, width: 150, height: 100)
        
        return imageView
    }

 

 

 

 

 


6 & 7 . ์ฒซ ๋ฒˆ์งธ ํ”ผ์ปค ๋ทฐ ์„ ํƒ ์‹œ ํŒŒ์ผ ๋ช… ์ถœ๋ ฅํ•˜๊ธฐ &๋‘ ๋ฒˆ์งธ ํ”ผ์ปค ๋ทฐ ์„ ํƒ ์‹œ ์ด๋ฏธ์ง€ ์ถœ๋ ฅํ•˜๊ธฐ

+ component๋Š” ์ธ๋ฐ์Šค 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. ์ธ์ˆ˜์ธ component๋ฅผ ์‚ฌ์šฉํ•ด component๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • didSelectRow ์ธ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” pickerView : ์‚ฌ์šฉ์ž๊ฐ€ ์ปดํฌ๋„ŒํŠธ์˜ ํ–‰์„ ์„ ํƒํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
    //์‚ฌ์šฉ์ž๊ฐ€ ํ”ผ์ปค ๋ทฐ์˜ ํ–‰์„ ์„ ํƒํ–ˆ์„ ๋•Œ ํ•  ์ผ์„ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ์—๊ฒŒ ์ง€์‹œํ•˜๋Š” ๋ฉ”์„œ๋“œ
    //didSelectRow ์ธ์ˆ˜๊ฐ€ ์žˆ๋Š” ๋ฉ”์„œ๋“œ
    func pickerView(_ pickerView: UIPickerView,
                    didSelectRow row: Int, inComponent component: Int) {
        //์™ผ์ชฝ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์„ ํƒํ–ˆ์„ ๋•Œ ํŒŒ์ผ ๋ช… ์ถœ๋ ฅ, ์˜ค๋ฅธ์ชฝ ์ปดํฌ๋„ŒํŠธ ์„ ํƒํ–ˆ์„ ๋•Œ ํŒŒ์ผ ๋ช… ์ถœ๋ ฅ
        if component == 0 {
            lblImageFileName.text  = imageFileName[row]
        }
        else{
            imageView.image = imageArray[row]
        }
    }

 

 

 

 

 

 


8. ํ”ผ์ปค ๋ทฐ ๋ฃฐ๋ › ํ–‰ ๋†’์ด ์„ค์ •

  • rowHeightForComponent ์ธ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” pickerView : ๊ฐ ์ปดํฌ๋„ŒํŠธ์˜ ํ–‰(์š”์†Œ) ๋†’์ด๋ฅผ ํ”ผ์ปค ๋ทฐ์— ์ „๋‹ฌํ•˜๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
    //returns height of row for each component.
    //component ํ–‰์˜ ๋†’์ด๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
    //rowHeightForComponent ์ธ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ๋ฉ”์„œ๋“œ
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component:Int) -> CGFloat {
        return PICKER_VIEW_HEIGHT
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๊ณต๋ถ€ํ•œ ์ž๋ฃŒ

https://zeddios.tistory.com/8
https://blog.naver.com/seotaji/220293615979
<๋ธ๋ฆฌ๊ฒŒ์ดํŠธ>
์•ผ๊ณฐ - ์Šค์œ„ํ”„ํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ 5ํŒ (SWIFT5)
https://blog.naver.com/seotaji/220287208861
<ํ”„๋กœํ† ์ฝœ>

Do It! ์Šค์œ„ํ”„ํŠธ๋กœ ์•„์ดํฐ ์•ฑ ๋งŒ๋“ค๊ธฐ ๊ฐœ์ • 5ํŒ
SWIFT ๋ฌธ์„œ
728x90