0

我在我身边。我的简单任务是使用 apickerView来设置一个默认值,如果它是以前设置的,当它关闭时。但问题出在任何逻辑上。它滚动到默认行并设置标题,但是当我尝试更改它时,它会滚动回默认值。我以为我可以设置一个布尔值条件,但现在无论我把它放在哪里都会影响。

任何帮助都会很棒。我在 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")

    }
4

0 回答 0