如何根据之前的选择器视图选择更新PickerView 数据
Posted
技术标签:
【中文标题】如何根据之前的选择器视图选择更新PickerView 数据【英文标题】:how to updatePickerView Data according to previous picker view selection 【发布时间】:2018-05-04 21:20:19 【问题描述】:我有 2 个pickerView。一个是显示课程,另一个是根据课程 ID(响应来自 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)
根据课程 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)
【问题讨论】:
【参考方案1】:您需要在选择课程后重新加载 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)
【讨论】:
以上是关于如何根据之前的选择器视图选择更新PickerView 数据的主要内容,如果未能解决你的问题,请参考以下文章