pickerview with CustomView


pickerView Swift 테스트 중 그냥 커스텀뷰까지 올린 구조로 만들어보았다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
//  ViewController.swift
//  testSwift2
//
//  Created by Eteris on 2018. 4. 24..
//  Copyright © 2018년 Eteris. All rights reserved.
//
 
import UIKit
 
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
{
    @IBOutlet weak var pv_test: UIPickerView!
    @IBOutlet weak var label_selectImageName: UILabel!
    @IBOutlet weak var iv_selectedImage: UIImageView!
    
    var array_imageData : Array<ImageData>?;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // ImageData
        array_imageData = Array<ImageData>();
        
        for index in 0 ... 2
        {
            
            var imageData : ImageData = ImageData.init(name: String(index+1+ ".jpg", desc: "desc " + String(index+1));
            
            array_imageData?.append(imageData);
        }
        
        for index in 0 ... array_imageData!.count-1
        {
            NSLog("index [" + String(index) + "] imageName : [" + array_imageData![index].imageName + "] / imageDesc : [" + array_imageData![index].imageDescription + "]");
        }
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
 
    
    // PICKER VIEW
    // 컴포넌트 수
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1;
    }
    
    // ROW 수
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int-> Int {
        return array_imageData!.count;
    }
    
//    // ROW별 타이틀
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        return array_imageData![row].imageName;
//    }
    
    // ROW 꾸미기
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
//        // 소스로
//        let iv = UIImageView.init(image: UIImage(named: array_imageData![row].imageName));
//
//        iv.frame = CGRect.init(x: 0, y: 0, width: 100, height: 100);
//
//        return iv;
        
        // 커스텀 뷰
        let rowView : row_pvItem = row_pvItem.init(frame: CGRect.init(x: 0, y: 0, width: 375, height: 200));
        
        rowView.row_iv.image = UIImage.init(named: array_imageData![row].imageName);
        rowView.row_label.text = array_imageData![row].imageDescription;
        
        return rowView;
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int-> CGFloat {
        return 200;
    }
    
    // 선택됨
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        label_selectImageName.text = array_imageData![row].imageDescription;
        iv_selectedImage.image = UIImage.init(named: array_imageData![row].imageName);
    }
}
 
 
cs


메인뷰는 이정도..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
//  row_pvItem.swift
//  testSwift2
//
//  Created by Eteris on 2018. 4. 30..
//  Copyright © 2018년 Eteris. All rights reserved.
//
 
import Foundation
import UIKit
 
class row_pvItem : UIView
{
    @IBOutlet var mainView: UIView!
    
    @IBOutlet weak var row_iv: UIImageView!
    @IBOutlet weak var row_label: UILabel!
    
    
    override init(frame: CGRect) { // for using CustomView in code
        super.init(frame: frame);
        self.commonInit();
    }
    
    required init?(coder aDecoder: NSCoder) { // for using CustomView in IB
        super.init(coder: aDecoder);
        self.commonInit();
    }
    
    private func commonInit()
    {
        // XIB Name
        Bundle.main.loadNibNamed("row_pvItem", owner: self, options: nil);
        
        guard let content = mainView else { return };
        content.frame = self.bounds;
        content.autoresizingMask = [.flexibleHeight, .flexibleWidth];
        self.addSubview(content);
    }
    
    func getHeight () -> CGFloat
    {
        return mainView.frame.size.height;
    }
    
    func getWidth () -> CGFloat
    {
        return mainView.frame.size.width;
    }
    
}
 
cs


커스텀뷰는 이렇게.


loadNibNamed에 xib 이름을 넣고 사용하면 된다.

'Developer > Swift' 카테고리의 다른 글

NavigationController / NavigationItem  (0) 2018.05.02
AlertDialog and Response Function  (0) 2018.05.02
pickerview with CustomView  (0) 2018.04.30
Extention  (0) 2018.04.20
Swift 각종 용어 정리 (지속 업데이트 필요)  (0) 2018.04.20
Protocol  (0) 2018.04.20

이 글을 공유하기


댓글(0)

Designed by CMSFactory.NET