как обновить данные выбора сборщика в соответствии с предыдущим выбором представления сборщика - PullRequest
0 голосов
/ 05 мая 2018

У меня есть 2 pickerView. один показывает курсы, а другой показывает разделы курсов в соответствии с идентификатором курсов (Ответ идет от Json), теперь проблема в том, что когда я выбираю курс (скажем, исчисление), а затем выбираю sectionPickerView, он дает мне список соответствующих разделов (A, B , C, D) но когда я снова изменяю курс на (английский), раздел PickerView по-прежнему показывает тот же раздел и не обновляет разделы в соответствии с Course Pickerview.

Вот мой код:

let coursesPicker = UIPickerView()
let sectionsPicker = UIPickerView()

var countryId = 0

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == coursesPicker {
        return GetCourses.instance.getCoursesInstance.count
    } else {
        return GetSectionService.sectionsServiceinstance.sectionModelInstance.count  
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == coursesPicker {
        return GetCourses.instance.getCoursesInstance[row].courseName
    } else {
        return GetSectionService.sectionsServiceinstance.sectionModelInstance[row].sectionName
    }        
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == coursesPicker {
        coursesTextField.text = GetCourses.instance.getCoursesInstance[row].courseName
        countryId = GetCourses.instance.getCoursesInstance[row].id
        self.callSectioApi(id: countryId)
        self.coursesPicker.isHidden = true
    } else {
        sectionTextField.text =  GetSectionService.sectionsServiceinstance.sectionModelInstance[row].sectionName
        self.sectionsPicker.isHidden = true
    }
    self.view.endEditing(true)
}

функция для обновления URL в соответствии с идентификатором курса

func callSectioApi(id : Int) {

    let Idurl = String(id)
    let url1 = getSectionUrl+Idurl
    print(url1)

    GetSectionService.sectionsServiceinstance.getSectionsName(url: url1) { (success) in
        if success {
        let status = GetSectionService.sectionsServiceinstance.status
            if status == 400 {
                print("400")
            } else if status == 500 {
                print("500")
            } else if status == 404 {
                print("404")
            } else if status == 200 {
                print("200")
                self.sectionTextField.text = ""
                self.sectionsPicker.reloadAllComponents()
            } else {
                print("Error")
            }
        } else {
            let status = GetSectionService.sectionsServiceinstance.status
            print(status)
        }
    }
}

1 Ответ

0 голосов
/ 08 мая 2018

Вам необходимо перезагрузить sectionPicker после выбора курса.

Итак, измените свой didSelect, как показано ниже

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

    if pickerView == coursesPicker {

        coursesTextField.text = GetCourses.instance.getCoursesInstance[row].courseName
        countryId = GetCourses.instance.getCoursesInstance[row].id
        self.callSectioApi(id: countryId)

        //Reload sectionPicker
        self.sectionsPicker.reloadAllComponents()
        self.sectionsPicker.selectRow(0, inComponent: 0, animated: false)

        self.coursesPicker.isHidden = true

    } else {

        sectionTextField.text =  GetSectionService.sectionsServiceinstance.sectionModelInstance[row].sectionName
        self.sectionsPicker.isHidden = true

    }
    self.view.endEditing(true)
}
...