一省艳、UIPickerView的使用和介紹
UIPickerView是一個選擇器控件亲茅,它比UIDatePicker更加通用沮脖,它可以生成單列的選擇器金矛,也可生成多列的選擇器,而且開發(fā)者完全可以自定義選擇項的外觀勺届,因此用法非常靈活。UIPickerView直接繼承了UIView娶耍,沒有繼承UIControl免姿,因此,它不能像UIControl那樣綁定事件處理方法榕酒,UIPickerView的事件處理由其委托對象完成胚膊。使用UIPickerView的對象應該遵守UIPickerViewDataSource,UIPickerViewDelegate。
二想鹰、UIPickerView 的初始化
let pickerView = UIPickerView.init(frame: CGRect.init(x: 100, y: 80, width: 200, height: 200))
/*
設置兩個代理
*/
pickerView.dataSource = self
pickerView.delegate = self
/*
設置背景色
*/
pickerView.backgroundColor = UIColor.gray
/*
是否顯示默認選中的Row背景處于高亮的狀態(tài)
false : 為默認
true : 高亮狀態(tài)
*/
pickerView.showsSelectionIndicator = true//(iOS13.0later被廢棄)
/*
渲染到視圖上
*/
self.view.addSubview(pickerView)
三紊婉、 UIPickerView的代理
// MARK: 代理事件的實現(xiàn)
// TODO:這是返回每一Row的寬度
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return 200.0
}
//TODO: 設置每一個Row的高度
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 60.0
}
// TODO:這是選擇哪一個Row,調(diào)用的函數(shù)
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("我選擇了第"+"\(row)"+"行")
}
// TODO: 返回每一個Row的文本
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return (SourceData.object(at: row) as! String)
}
// TODO: 可以設置哪一行顯示特定的樣式
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
// 創(chuàng)建一個對象
let specificView = UIView.init()
specificView.frame = CGRect.init(x: 10, y: 5, width: 100, height: 60)
specificView.backgroundColor = UIColor.magenta
/**
創(chuàng)建一個標題
*/
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 : 設置每個Row的文本顯示的樣式參數(shù)
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
/**
我們要第四行,顯示紅色辑舷,并18號字體
*/
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ù)
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return SourceData.count
}
四喻犁、設置數(shù)據(jù)
func createDataSource() {
SourceData = ["安其拉","白起","不知火舞","妲己","狄仁杰","典韋","韓信","老夫子","劉邦","劉禪","魯班七號","墨子","孫臏","孫尚香","孫悟空","項羽","亞瑟","周瑜"]
}
五 、UIPickerView的一些get 屬性介紹
/**
`獲取返回組的個數(shù)`
*/
let rowCount =pickerView.numberOfComponents
print(rowCount)
/**
`設置指定組何缓,返回的row的個數(shù)`
*/
pickerView.numberOfRows(inComponent: 1)
/**
`獲取某一組的大小`
*/
let componentSize = pickerView.rowSize(forComponent: 1)
print(componentSize.width,componentSize.height)
4肢础、獲取指定組的指定列的View
/**
獲取指定組和指定列的View
*/
let NwComponentRowView = pickerView.view(forRow: 2, forComponent: 1)
NwComponentRowView?.backgroundColor = UIColor.brown
5、UIPickerView的數(shù)據(jù)更新
/**
` 更新所有組的數(shù)據(jù)`
*/
pickerView.reloadAllComponents()
/**
`更新指定組的數(shù)據(jù)`
*/
pickerView.reloadComponent(1)
/**
`指定選中某一組的某一行`
*/
pickerView.selectRow(3, inComponent: 1, animated: true)
/**
`獲取指定組的哪一行被選中`
*/
let NwSelecdRow = pickerView.selectedRow(inComponent: 1)
print(NwSelecdRow)