iOS uipickerview第二个组件显示先前选择的结果

时间:2017-11-08 09:32:49

标签: ios swift uipickerview

我正在尝试制作允许您选择手机的UIPickerView。 它有两个组件,您首先选择品牌,然后选择手机。 它的工作原理非常完美但是当你选择一个手机很少的品牌时,有一个问题就是一个或两个,UIpickerView第二个组件显示了以前品牌的一些手机(如果有更多的手机),即使你不能选择它们。

这是拥有更多手机的品牌

here is the brand that has more mobiles

这就是当您选择拥有一部手机的品牌后会发生什么

enter image description here

这是代码

var brands : [Brand] = []{
        didSet
        {
            self.pick.reloadAllComponents()
        }
    }

didselectrow功能

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            if component == 1 {
                self.spinin.startAnimating()
                loadMobiles(brand_id: String(brands[row].id))
            } else{
                id1 = mobiles[row].id
                print(id1)
            }
 }

并加载手机功能

func loadMobiles(brand_id: String)
    {
        let url = URL(string: "http://myapi.com?id=\(brand_id)")!
        let task = session.dataTask(with: url) { (data, response, error) in
            guard let data = data else { return }
            do{
                self.mobiles = try parse(data)
                DispatchQueue.main.async {
                    self.pick.reloadAllComponents()
                    self.pick.selectRow(0, inComponent: 0, animated: true)
                    self.spinin.stopAnimating()
                }
            }
            catch{
                print("JSONParsing Error :\(error)")
            }

  }
        task.resume()
          }

我该如何解决这个问题?

0 个答案:

没有答案
相关问题