json - 选择器视图:当从选择器视图 1 中选择数据时,我希望选择器视图 2 中的数据随选择器视图 1 更改
问题描述
我对选择器视图有一些疑问 我有两个选择器视图 选择器视图 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
}
解决方案
您在现有代码中几乎就在那里 - 只需使用该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
}
}
}
推荐阅读
- python - Pytest - 将夹具传递给 mark.parametrize
- react-native - 通过 onPress 传递数据不允许在函数中正确传递参数
- jquery - jquery 文件上传不适用于 bootstrap v4.1.3
- javascript - 将响应从 fetch() 转换为 JSON 对象
- c - 限制输入字符串字符
- python - 在 Python 中访问 PowerShell 参数
- flutter - 颤动如何为 GridView.Builder 的孩子提供高度
- java - 使用 Spring Security 时引导程序不起作用
- iso - VMDK 到 ISO 文件转换器
- javascript - 带有来自 fetch() 的数组的 React16.4 setState 不起作用