UIPickView簡(jiǎn)介

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!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容