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

标签 json swift alamofire uipickerview swifty-json

我有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/

相关文章:

php - Json编码带空格的字符串

javascript - 使用 JQuery 搜索 JSON 对象

ios - AFNetworking 同时请求

iOS keyboardWasShown、NSNotification 和 Optional 问题

xcode - 如果条件因表达式太复杂而失败

ios - 获取 json 响应但无法解析为标签

c# - ASP.NET 5 : How to reload strongly typed configuration on change

swift - 获取在 ViewController 类中启动应用程序的文件名?

swift - 在 MacOS 中下载 Alamofire。看起来不错,进度正常,但没有文件?

ios - swift 2.1 alamofire超时方法