我对选择器视图有疑问我有两个选择器视图 选择器视图1 = getpPlandateDCPlanData 选择器视图2 = getpShipmentDCPlanData
我希望从选择器视图1中选择数据时,选择器视图2中的数据随选择器视图1发生变化
var getpPlandateDCPlanData = ["20190118","20190119"]
var getpShipmentDCPlanData = ["4505023244","4505023274"]
// Picker view function
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == pickerview1 {
return self.getpPlandateDCPlanData.count
}
else if pickerView == pickerview2{
return self.getpShipmentDCPlanData.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pickerview1{
return getpPlandateDCPlanData[row]
//return self.convertToString(dateString: getpPlandateDCPlanData[row], formatIn: "yyyyMMdd", formatOut: "MMM dd, yyyy")
}
else if pickerView == pickerview2{
return getpShipmentDCPlanData[row]
}
return nil;
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//if getpPlandateDCPlanData.count > 0 && getpPlandateDCPlanData.count >= row && getpShipmentDCPlanData.count >= row {
if getpPlandateDCPlanData.count >= row && getpShipmentDCPlanData.count >= row {
if pickerView == pickerview1 {
getpPlanDatePickerView.text = getpPlandateDCPlanData[row]
print("PlanDatePickerView ==> \(getpPlandateDCPlanData)")
}
else if pickerView == pickerview2 {
getpShipmentPickerView.text = getpShipmentDCPlanData[row]
print("ShipmentPickerView ==> \(getpShipmentDCPlanData)")
}//if
}//if
}
答案 0 :(得分:0)
您的现有代码几乎已经存在-只需使用didSelectRow
方法来检测pickerView1
中的更改,然后在pickerView2
中设置该行。我在下面添加了一个简单的示例,该示例只是将pickerView 2中的行交换为pickerView1的相反行。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView === pickerView1 {
switch row {
case 0: pickerView2.selectRow(1, inComponent: 0, animated: true)
case 1: pickerView2.selectRow(0, inComponent: 0, animated: true)
default: break
}
}
}