如何根据先前的选择器视图选择

时间:2018-05-04 21:20:19

标签: json swift alamofire uipickerview swifty-json

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

更新网址的功能
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 个答案:

答案 0 :(得分:0)

您需要在选择课程后重新加载sectionPicker。

所以,改变你的didSelect如下

abc,def,"ghi,jkl,mno",pqr,stu