如何根据之前的picker视图选择更新PickerView数据
how to updatePickerView Data according to previous picker view selection
我有 2 个 pickerView。一个正在显示课程,另一个正在根据课程 ID 显示课程部分(响应来自 Json)现在问题是当我选择课程(比如说微积分)然后 select 它给我的 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)
}
根据课程 ID
更新 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)
}
}
}
您需要在选择课程后重新加载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)
}
我有 2 个 pickerView。一个正在显示课程,另一个正在根据课程 ID 显示课程部分(响应来自 Json)现在问题是当我选择课程(比如说微积分)然后 select 它给我的 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)
}
根据课程 ID
更新 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)
}
}
}
您需要在选择课程后重新加载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)
}