iOS开发——UI篇Swift篇&UIPickerView

UIPickerView

 

  1     //返回按钮事件
  2     @IBAction func backButtonClick()
  3     {
  4         self.navigationController?.popViewControllerAnimated(true)
  5     }
  6     
  7     var authorsArray:[String]!
  8     var appsArray:[String]!
  9     
 10     
 11     override func viewDidLoad() {
 12         super.viewDidLoad()
 13         
 14         titleLabel.text = titleString
 15         
 16         
 17         // Do any additional setup after loading the view.
 18         
 19         //创建选择控制器
 20         var pickerView:UIPickerView = UIPickerView(frame: CGRectMake(0, 200, 0, 0))
 21         
 22         //添加到视图上
 23         self.view.addSubview(pickerView)
 24         
 25         //指定数据源
 26         pickerView.dataSource = self
 27         
 28         //指定代理
 29         pickerView.delegate = self
 30         
 31         //创建2个数组
 32         authorsArray = ["刘明洋","汪鸿俊","任小蕾"]
 33         appsArray = ["租房点评","哪里逃","AllOff","iJump","数独"]
 34         
 35         
 36         
 37         
 38         //常用的一些属性和方法介绍
 39         
 40         
 41         
 42 
 43         
 44         
 45     }
 46 
 47     override func didReceiveMemoryWarning() {
 48         super.didReceiveMemoryWarning()
 49         // Dispose of any resources that can be recreated.
 50     }
 51     
 52 
 53     /*
 54     // MARK: - Navigation
 55 
 56     // In a storyboard-based application, you will often want to do a little preparation before navigation
 57     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
 58         // Get the new view controller using segue.destinationViewController.
 59         // Pass the selected object to the new view controller.
 60     }
 61     */
 62 
 63     
 64     // MARK: - UIPickerViewDataSource
 65 
 66     //返回多少列
 67     func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
 68     {
 69         return 2
 70     }
 71     
 72     //每一列返回多少行
 73     func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
 74     {
 75         //第1列返回authorsArray的个数
 76         if component == 0{
 77             return authorsArray.count
 78         }
 79         //第2列返回appsArray的个数
 80         else if component == 1 {
 81             return appsArray.count
 82         }
 83         
 84         return 0
 85     }
 86     
 87     // MARK: - UIPickerViewDelegate
 88     
 89     //通过字符串来创建选择控制器
 90 //    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
 91 //    {
 92 //        //第1列返回authorsArray对应的值
 93 //        if component == 0{
 94 //            return authorsArray[row]
 95 //        }
 96 //        
 97 //            //第2列返回appsArray对应的值
 98 //        else if component == 1 {
 99 //            return appsArray[row]
100 //        }
101 //        
102 //        return ""
103 //    }
104     
105     //通过View来创建带有图片的选择控制器
106     func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
107     {
108         var view:UIView = UIView(frame: CGRectMake(0, 0, 200, 40))
109         
110         //创建图像视图
111         var imageView = UIImageView(frame: CGRectMake(0, 0, 40, 40))
112         imageView.image = UIImage(named: "cellImage.png")
113         
114         
115         //创建文本标签
116         var label:UILabel = UILabel(frame: CGRectMake(40, 0, 160, 40))
117         //第1列返回authorsArray对应的值
118         if component == 0{
119             label.text =  authorsArray[row]
120         }
121 
122         //第2列返回appsArray对应的值
123         else if component == 1 {
124             label.text =  appsArray[row]
125         }
126         
127         
128         
129         //添加图像
130         view.addSubview(imageView)
131         
132         //添加文本标签
133         view.addSubview(label)
134         
135         return view
136     }
137     
138     
139     //选项改变后,触发代理事件
140     func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
141     {
142         println("当前选择的是第\(component)列   第\(row)行")
143     }

 

 

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。