Swift:Json在两个选择器中解析国家和城市之间的视图 [英] Swift: Json parse in two pickerView between country and city

查看:113
本文介绍了Swift:Json在两个选择器中解析国家和城市之间的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个JSON网址.要在两个UIPickerView之间进行解析.一个UIPickerView用于国家(地区),另一个用于城市.我已经能够在Country的第一个UIPickerView中解析JSON,这里的数据非常完美.但是单击该城市的UIPickerView时,我在DidSelectRow函数中收到了一条线程消息.请尝试解决此问题

I have two JSON Url. want to parse between two UIPickerView. one UIPickerView for Country, and another one for the city. I have been able to parse JSON in first UIPickerView of Country, in here data is coming perfectly. but I got a thread message in DidSelectRow func when clicking UIPickerView of the city. please try to solve this problem

这是我的代码.

var arrCountryPicker = [CountryPicker]()

 var arrCitiPicker = [CityPicker]()

    var id = 0
    var country_id = 0

我的两个网址

let url = URL(string: "http://......./mobile-app/countries")

let url = URL(string: "http://......../mobile-app/city-by-country-id?country_id="+String(country_id))

PickerViewCode

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {


    var country: Int = arrCountryPicker.count

    if pickerView == cityPickerView {

        country = self.arrCitiPicker.count
    }
    return country
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    if pickerView == countryPickerView {

        let titleRow = arrCountryPicker[row].name

        return titleRow

    } else if pickerView == cityPickerView{
        let titleRow = arrCitiPicker[row].name

        return titleRow
    }
    return ""
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    countryTextField.text = arrCountryPicker[row].name

    if pickerView == countryPickerView {
        self.countryTextField.text = self.arrCountryPicker[row].name
        self.country_id = self.arrCountryPicker[row].id!
        self.countryPickerView.isHidden = true
    }
    else if pickerView == cityPickerView{

有关降级代码的线程是线程1:exc_bad_instruction(code = exc_1386_invop,subcode = 0x0)

        self.cityTextField.text = self.arrCitiPicker[row].name

        self.cityPickerView.isHidden = true

    }
    self.view.endEditing(true)
}


func textFieldDidBeginEditing(_ textField: UITextField) {

    if (textField == countryTextField){
        self.countryPickerView.isHidden = false

    }
    else if (textField == cityTextField){
        self.cityPickerView.isHidden = false

    }

}

推荐答案

更改国家/地区时,您需要更新城市选择器的数组.

You need to update array of city picker whenever you change Country.

在没有选择国家/地区选择器"的情况下,为特定国家/地区ID调用网络服务并更新城市阵列.

In did select of Country picker, Call web service for particular country id and update array of Cities.

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    //Remove this line
    //txtCountry.text = self.arrCountries[row].name

    if pickerView == CountryPicker {
        self.txtCountry.text = self.arrCountries[row].name
        self.country_id = self.arrCountryPicker[row].id!

        //Update city array according selected country
        self.updateCitiesFor(id: row)

        self.CountryPicker.isHidden = true
    }  else if pickerView == cityPicker {
        self.txtCity.text = self.arrCities[row].name
        self.cityPicker.isHidden = true
    }
    self.view.endEditing(true)
}

用于更新arrCitiPicker的功能

func for update arrCitiPicker

func updateCitiesFor(id: Int) {

    let url = URL(string: "http://......../mobile-app/\(id)")

    //Call web service and store cities data of selected country into arrCitiPicker

    //Clear city textfield when country change
    self.txtCity.text = ""     

    //Update City Picker with new data
    cityPicker.reloadAllComponents()
}

这篇关于Swift:Json在两个选择器中解析国家和城市之间的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆