一呀洲、UIPickerView的使用和介紹
UIPickerView是一個(gè)選擇器控件舅柜,它比UIDatePicker更加通用完丽,它可以生成單列的選擇器,也可生成多列的選擇器乡洼,而且開發(fā)者完全可以自定義選擇項(xiàng)的外觀崇裁,因此用法非常靈活匕坯。UIPickerView直接繼承了UIView,沒有繼承UIControl拔稳,因此葛峻,它不能像UIControl那樣綁定事件處理方法,UIPickerView的事件處理由其委托對(duì)象完成巴比。使用UIPickerView的對(duì)象應(yīng)該遵守UIPickerViewDataSource,UIPickerViewDelegate术奖。
二、UIPickerView 的初始化
對(duì)象
let NwPickerView = UIPickerView.init(frame: CGRect.init(x: 100, y: 80, width: 200, height: 200))
/**
設(shè)置兩個(gè)代理
*/
NwPickerView.dataSource = self
NwPickerView.delegate = self
/**
設(shè)置背景色
*/
NwPickerView.backgroundColor = UIColor.gray
/**
是否顯示默認(rèn)選中的Row背景處于高亮的狀態(tài)
false : 為默認(rèn)
true : 高亮狀態(tài)
*/
NwPickerView.showsSelectionIndicator = true
/**
渲染到視圖上
*/
self.view.addSubview(NwPickerView)
三轻绞、 UIPickerView的代理
/**
設(shè)置兩個(gè)代理
*/
NwPickerView.dataSource = self
NwPickerView.delegate = self
代理事件觸發(fā)的函數(shù)
// MARK: 代理事件的實(shí)現(xiàn)
// TODO:這是返回每一Row的寬度
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return 200.0
}
//TODO: 設(shè)置每一個(gè)Row的高度
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 60.0
}
// TODO:這是選擇哪一個(gè)Row,調(diào)用的函數(shù)
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("我選擇了第"+"\(row)"+"行")
}
// TODO: 返回每一個(gè)Row的文本
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return (SourceData.object(at: row) as! String)
}
// TODO: 可以設(shè)置哪一行顯示特定的樣式
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
// 創(chuàng)建一個(gè)對(duì)象
let specificView = UIView.init()
specificView.frame = CGRect.init(x: 10, y: 5, width: 100, height: 60)
specificView.backgroundColor = UIColor.magenta
/**
創(chuàng)建一個(gè)標(biāo)題
*/
let specificLable = UILabel.init(frame: CGRect.init(x: 5, y: 0, width: 90, height: 60))
specificLable.text = (SourceData[row] as! String)
specificLable.textColor = UIColor.white
specificView.addSubview(specificLable)
return specificView
}
// TODO : 設(shè)置每個(gè)Row的文本顯示的樣式參數(shù)
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
/**
我們要第四行采记,顯示紅色,并18號(hào)字體
*/
let attributedString = NSAttributedString.init(string:(SourceData[row] as! String), attributes: [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 18),NSForegroundColorAttributeName:UIColor.red])
return attributedString
}
//MARK : UIPickerViewDataSource 的代理事件
// TODO: 返回列表的組數(shù)
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// TODO : 這是設(shè)置返回列表的個(gè)數(shù)
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return SourceData.count
}
四政勃、設(shè)置數(shù)據(jù)
func createDataSource() {
SourceData = ["安其拉","白起","不知火舞","妲己","狄仁杰","典韋","韓信","老夫子","劉邦","劉禪","魯班七號(hào)","墨子","孫臏","孫尚香","孫悟空","項(xiàng)羽","亞瑟","周瑜"]
}
五 唧龄、UIPickerView的一些get 屬性介紹
1、獲取返回組的個(gè)數(shù)
/**
獲取返回組的個(gè)數(shù)
*/
let rowCount = NwPickerView.numberOfComponents
print(rowCount)
2奸远、獲取指定組返回的列表個(gè)數(shù)
/**
設(shè)置指定組既棺,返回的row的個(gè)數(shù)
*/
NwPickerView.numberOfRows(inComponent: 1)
3、獲取某一列表的大小
/**
獲取某一組的大小
*/
let componentSize = NwPickerView.rowSize(forComponent: 1)
print(componentSize.width,componentSize.height)
4然走、獲取指定組的指定列的View
/**
獲取指定組和指定列的View
*/
let NwComponentRowView = NwPickerView.view(forRow: 2, forComponent: 1)
NwComponentRowView?.backgroundColor = UIColor.brown
5援制、UIPickerView的數(shù)據(jù)更新
1>全部更新所有的組
/**
更新所有組的數(shù)據(jù)
*/
NwPickerView.reloadAllComponents()
2>更新指定的組的數(shù)據(jù)
/**
更新指定組的數(shù)據(jù)
*/
NwPickerView.reloadComponent(1)
6、指定選中某一組的某一行
/**
指定選中某一組的某一行
*/
NwPickerView.selectRow(3, inComponent: 1, animated: true)
7芍瑞、獲取指定組的哪一行被選中
/**
獲取指定組的哪一行被選中
*/
let NwSelecdRow = NwPickerView.selectedRow(inComponent: 1)
print(NwSelecdRow)
六晨仑、效果展示
1>默認(rèn)效果
Simulator Screen Shot 2017年6月23日 上午11.45.34.png
2>從新設(shè)置View的樣式
Simulator Screen Shot 2017年6月23日 下午12.01.38.png