iOS開發——UI篇Swift篇&UIPickerView

來源:互聯網
上載者:User

標籤:

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!) -> UIView107     {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 view136     }137     138     139     //選項改變後,觸發代理事件140     func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)141     {142         println("當前選擇的是第\(component)列   第\(row)行")143     }

 

 

 

 

iOS開發——UI篇Swift篇&UIPickerView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.