UIPickerView默认回滚已更改

时间:2018-12-11 08:18:08

标签: swift uipickerviewcontroller pickerview

我在自己身边。我的简单任务是使用pickerView设置一个默认值(如果它是在关闭时预先设置的)。但是问题出在纽约逻辑上。它滚动到默认行并设置标题,但是当我尝试更改它时,它会滚动回到十进制值。我以为我可以设置一个布尔值条件,但是无论我把它放在哪里,现在都已经影响了。

任何帮助都会很棒。我在Swift中没有足够的基础知识,无法知道控件在哪里以正确的方式执行此操作。这是它的肉。

/选择器视图功能...

func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return pickerData.count
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if (mySavedBool) {     //this is true 
        let pickerIndexVal = pickerViewTitty(sourceNewSelected: mySavedSource)
        pickerView.selectRow(pickerIndexVal, inComponent: 0, animated: true)
        }
        print(pickerData[row].id)
        NameLabel.text = pickerData[row].name
        return pickerData[row].name

    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
            NameLabel.text = pickerData[row].name
            NameLabelID.text = pickerData[row].id
            mySavedSource = pickerData[row].id          //Set the source equal to the source id needed in the URL

            UserDefaults.standard.set(mySavedSource, forKey: "mySavedSource")       //set the selection to save in the pfile

                print("Inside pickerView mySavedBool = \(mySavedBool) and Plist defaults in picker \(mySavedSource)")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        print("On viewDidLoad mySavedSource is \(mySavedSource) and mySavedBoolTest is  \(mySavedBool)")

        tableView.refreshControl = refreshControl
        refreshControl.addTarget(self, action: #selector(ViewController.refreshData), for: UIControl.Event.valueChanged)

        myPicker.delegate = self
        myPicker.dataSource = self
        tableView.dataSource = self
        tableView.delegate = self

        fetchURLSource()
        fetchURLData()

    }

@objc func refreshData () {

        print("refresh Data Called and mySavedBoolTest \(mySavedBool)")
        print("source Selected at start of refresh \(mySavedSource)")

        fetchURLData()
        tableView.reloadData()
        refreshControl.endRefreshing()
        print("Looks Done")

    }

0 个答案:

没有答案