首页 > 解决方案 > 选择器视图:当从选择器视图 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
    }

标签: jsonswiftxcode

解决方案


您在现有代码中几乎就在那里 - 只需使用该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
         }
      }
   }

推荐阅读