gpt4 book ai didi

json - 如何根据之前的选择器 View 选择更新PickerView数据

转载 作者:行者123 更新时间:2023-11-30 11:31:44 27 4
gpt4 key购买 nike

我有2个pickerView。一个是显示类(class),另一个是根据类(class) id 显示类(class)部分(响应来自 Json)现在的问题是当我选择类(class)(比如说微积分),然后选择sectionPickerView 它给我相应部分的列表(A,B ,C,D),但是当我再次将类(class)更改为(英语)时,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)
}

根据类(class)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)
}

关于json - 如何根据之前的选择器 View 选择更新PickerView数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50183182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com