一 UIPickerView是一种选择控制器
1 它继承与UIView 需要遵从 UIPickerViewDelegate,UIPickerViewDataSource 俩个代理
2 创建pickerView
func loadSite() {
pickerView = UIPickerView()
view.addSubview(pickerView)
// 设置代理和数据源
pickerView.delegate = self
pickerView.dataSource = self
// 设置选择框的默认值
pickerView.selectRow(2, inComponent: 0, animated: true)
pickerView.selectRow(3, inComponent: 1, animated: true)
// 约束 根据自己的情况适配
pickerView.snp.makeConstraints { (make) in
make.bottom.left.right.equalTo(view)
make.height.equalTo(150)
}
}
// MARK: Picker Delegate 实现代理方法
func numberOfComponents(in pickerView: UIPickerView) -> Int {
//返回多少列
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//返回每一列有多少行
if 0 == component {
return areaArray.count
}else {
return cityArray.count
}
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
//每行多高
return 40
}
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
// 每列多宽
return 100
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//赋值
if 0 == component {
let dic = areaArray[row] as! Dictionary<String,AnyObject> as! NSMutableDictionary
return (dic["state"] as? String)
}else {
let area = cityArray[row] as! String
return (area)
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//选中所执行的方法
if 0 == component {
}else {
}
}
网友评论