2 PickerView

时间:2017-10-18 09:00:45

标签: swift uipickerview

我在我的应用程序中使用了2个Pickerview。但是,他们的信息会在使用时混合。例如:如果我在第一个选择器中选择2.row,那么系统会将第二个选择器行更改为2,反之则相同。

我的代码在这里:

    override func viewDidLoad() {
    super.viewDidLoad()

    petArray = ["A","B","C","D"]

    feedTimeArray = ["1","2","3","4"]

}    

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView.tag == 1{
        return petArray.count
    }
    if pickerView.tag == 2 {
        return feedTimeArray.count
    }
    return 0
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView.tag == 1 {
        return petArray[row]
    }
    if pickerView.tag == 2{
        return feedTimeArray[row]
    }
    return ""
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    selectedFeed = feedTimeArray[row]
    print(feedTimeArray[row])

    if selectedFeed == feedTimeArray[1] {
        //case 1
    }

    if selectedFeed == feedTimeArray[2] {
        //case 2
    }
    if selectedFeed == feedTimeArray[3] {
        //case 3
    }

    selectedPet = petArray[row]
    print(petArray[row]) }

示例:

我选择了B. 系统将第二个选择器更改为2

我选择了4 系统将第一个选择器更改为D

1 个答案:

答案 0 :(得分:1)

我认为您的didSelectRow方法会像这样

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

 if pickerView.tag == 1
 {
    selectedFeed = feedTimeArray[row]
    print(feedTimeArray[row])

     if selectedFeed == feedTimeArray[1] {
     //case 1
     }

      if selectedFeed == feedTimeArray[2] {
      //case 2
     }
     if selectedFeed == feedTimeArray[3] {
     //case 3
     }
 }
 else if pickerView.tag == 2
 {
     selectedPet = petArray[row]
     print(petArray[row]) 
 }
 else
   {   }
}