码迷,mamicode.com
首页 > 编程语言 > 详细

Swift - UIPickerView

时间:2019-01-07 01:33:52      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:center   ted   print   ber   大小   ==   using   sub   状态   

import UIKit

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
    var pickerView:UIPickerView!
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView = UIPickerView(frame: CGRect(x: 0, y: 250, width: 350, height:100))
        //delegate设为自己
        pickerView.delegate = self
        //DataSource设为自己
        pickerView.dataSource = self
        //设置PickerView默认值
        pickerView.selectRow(1, inComponent: 0, animated: true)
        self.view.addSubview(pickerView)
        
        let button = UIButton(frame:CGRect(x: 0, y: 0, width: 100, height: 30))
        button.frame = CGRect(x: 150, y: 400, width: 50, height: 50)
        button.backgroundColor = UIColor.red
        button.setTitle("确定", for: .normal)
        button.addTarget(self, action: #selector(ViewController.getPickerViewValue), for: .touchUpInside)
        self.view.addSubview(button)
    }
    @objc func getPickerViewValue(){
        let message = String(pickerView.selectedRow(inComponent: 0))
        let alret = UIAlertController(title: "选择", message: message, preferredStyle: .alert)
        self.present(alret, animated: true, completion: nil)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            self.presentedViewController?.dismiss(animated: true, completion: nil)
        }
    }
    //设置PickerView列数(dataSourse协议)
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    //设置PickerView行数(dataSourse协议)
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 9
    }
    //设置PickerView选项内容(delegate协议)
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return String(row)+"-"+String(component)
    }
    //设置列宽
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        if component == 0{
            return 100
        }
        else{
            return 200
        }
    }
    //设置行高
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    //血钙PickerView选项
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        //将图片设为PickerView选型
        let image = UIImage(named: "icon"+String(row))
        let imageView = UIImageView(image:image)
        //修改字体,大小,颜色
        var pickerLabel = view as? UILabel
        if pickerLabel == nil{
            pickerLabel = UILabel()
            pickerLabel?.font = UIFont.systemFont(ofSize: 16)
            pickerLabel?.textAlignment = .center
        }
        pickerLabel?.text = String(row)+"-"+String(component)
        pickerLabel?.textColor = UIColor.blue
        return imageView//pickerLabel!(选择其一)
    }
    //检测响应选项的选择状态
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print(String(row)+"-"+String(component))
    }
}

Swift - UIPickerView

标签:center   ted   print   ber   大小   ==   using   sub   状态   

原文地址:https://www.cnblogs.com/iOS-Development/p/10230997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!