I have 2 pickerView. one is showing courses and other is showing courses sections according to courses id(Response is going from Json) now the problem is when I choose course(lets say calculus) and then select the sectionPickerView it gives me list of corresponding sections(A,B,C,D) but when I change the course again to (English) the section PickerView still shows the same section and doesn't update sections according to Course Pickerview.
Here is my Code :
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)
}
function to update Url according to course Id
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)
}
}
}
You need to reload sectionPicker after course selection.
So, change your didSelect like below
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)
}