0

I am beside myself. My simple task is to use a pickerView to set a default value if it was previouly set When it closed. But the problem is in ny logic. It rolls to the default row and sets the title but when I try to change it, it scrolls back to the decault value. I thought I could set a boolean value condition but that has now affect no matter where I put it.

Any help would be great. I have not had enough fundamentals in Swift to know where the control is to do this the right way. Here is the meat of it.

/Picker View Functions ...

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")

    }
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Belga
  • 1
  • 2

0 Answers0