UIPickView
1. 效果圖:

我們今天要做的就是這樣的一個(gè)效果
而這個(gè)控件一般是用在用戶填寫個(gè)人信息(生日, 所屬地)等等
其實(shí)這個(gè)控件和我們的UITableView差不多
也是需要設(shè)置數(shù)據(jù)源, 代理
2. 步驟:
2.1. 拖控件

2.2. 連線, 寫代碼
在此之前, 我們先介紹一下, 這個(gè)UIPlickView的數(shù)據(jù)源方法, 以及代理方法 :
2.3遵守協(xié)議
<UIPickerViewDataSource,UIPickerViewDelegate>
2.4拖線, 設(shè)置數(shù)據(jù)源, 代理:
//1.設(shè)置數(shù)據(jù)源
pickView.dataSource = self
//2.設(shè)置代理
pickView.delegate = self
2.5數(shù)據(jù)源方法:
//總共有多少列
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
//每一組總共多少行
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 10
}
//第一行展示什么內(nèi)容
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "gj"
}
2.6代理方法
// 每一列的高度
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 35
}
// 每一列的寬度
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return 34
}
// 第Componet行展示的哪一個(gè)控件
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
<#code#>
}
// 選中當(dāng)前那一行
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
<#code#>
}
// 第Component的列每一行展示什么標(biāo)題
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
<#code#>
}
}
當(dāng)然 , 上面的所有的方法我不可能都使用
所以下面就是我們做好上面的那個(gè)界面就可以了
首先,依舊是plist文件
private lazy var foodList:[[String]] = {
let filePath = NSBundle.mainBundle().pathForResource("foods.plist", ofType: nil)
return NSArray(contentsOfFile: filePath!) as! [[String]]
}()
其次, 實(shí)現(xiàn)數(shù)據(jù)源方法
//總共有多少列
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return foodList.count
}
//每一列有多少行
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let array = foodList[component]
return array.count
}
//第一行展會(huì)什么內(nèi)容
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let array = foodList[component]
return array[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
chooseFooldLabel.text = foodList[component][row]
}
2.7 結(jié)束
注意的是我們這個(gè)控制器有三個(gè)屬性:
@IBOutlet weak var chooseFooldLabel: UILabel!
@IBOutlet weak var pickView: UIPickerView!