在我的选择器视图中数组数据没有加载?

时间:2017-07-06 13:54:08

标签: ios swift3 uipickerview

在此我尝试用数据加载选择器视图但不能加载数据,它显示如下图所示只显示空选择器视图我试图从很长一段时间可以帮助我什么&#39执行错误?
enter image description here

    var firstName : String = ""
    var lastName : String = ""
    var mobileNumber : Int = 0
    var email : String = ""
    var defaultValue = 1
    let url = "http://www.json-generator.com/api/json/get/bPmyumGWeW?indent=2"
    var dateofbirth : String = ""
    var Gender : String = ""
    var genderArray = [String]()

        override func viewDidLoad() {
        super.viewDidLoad()
        let picker = UIPickerView()
        picker.isHidden = true
        picker.delegate = self
        DateofBirthTextfield.delegate = self
        selectGenderTextField.inputView = picker
        MyaccountJsonWithURL()
        genderArray = ["Male","Female"]

    }

    func MyaccountJsonWithURL() {
        let url = NSURL(string: self.url)
        URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
            if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
                //  print(jsonObj!.value(forKey: "Detail"))
                if let profileArray = jsonObj!.value(forKey: "profile") as? NSArray {
                    for array in profileArray {
                        if let profileDict = array as? NSDictionary {
                            if let firstName = profileDict.value(forKey: "first name"){
                                self.firstName.append(firstName as! String)
                            }
                            if let lastName = profileDict.value(forKey: "last name"){
                                self.lastName.append(lastName as! String)
                            }
                            self.mobileNumber = profileDict.value(forKey: "mobile") as! Int
                            print(self.mobileNumber)
                            if let emailId = profileDict.value(forKey: "email"){
                                self.email.append(emailId as! String)
                            }
                            self.dateofbirth = profileDict.value(forKey: "dob") as! String
                            if let gender = profileDict.value(forKey: "gender"){
                                self.Gender.append(gender as! String)
                            }
                        }
                    }
                }
                OperationQueue.main.addOperation({
                    self.FirstNameTextfield.text = self.firstName
                    self.LastNameTextfield.text = self.lastName
                    self.MobileNumberTextfield.text = String(describing: self.mobileNumber)
                    self.EmailIdTextfield.text = self.email
                    self.DateofBirthTextfield.text = String(describing: self.dateofbirth)
                    self.selectGenderTextField.text = self.Gender
                })
            }
        }).resume()
    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return genderArray.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return genderArray[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectGenderTextField.text = genderArray[row]
        self.view.endEditing(false)
    }

2 个答案:

答案 0 :(得分:1)

类型&替换picker.hidden = true

picker.hidden = false

答案 1 :(得分:0)

viewDidLoad中,您将选择器设置为hidden。设置picker.isHidden = false以显示它