Derek N
Derek N

Reputation: 49

Trying to reload all data for pickerview not working

I developed an app where I have two labels, two pickerviews and a segmented control button that changes the labels between Imperial and Metric and based on that label, different arrays will load in each pickerview. This currently all works. The issue i'm having is since each pickerview is an array of weight and height, when the user presses the segmented control button to change the measurement type it changes the index value of the 2nd array. For instance if i'm currently on the imperial scale and i select 105 (starts at 100), if i change over to metric, it adds the correct array BUT it auto increases my starting number by an index of 5 (difference between my starting number and the number i selected from the pickerview and vice versa.

Here is my selector code:

@IBAction func selectorChoice(_ sender: Any) {
        if selectorLabel.selectedSegmentIndex == 0 {
            heightPicker.reloadAllComponents()
            weightPicker.reloadAllComponents()
            heightLabel.text = "Height (in)"
            weightLabel.text = "Weight (lbs)"
            weightInput.text = "100"
            heightInput.text = "48"
            UserDefaults.standard.set(selectorLabel.selectedSegmentIndex, forKey: "stateSelected")
            UserDefaults.standard.set(heightLabel.text!, forKey: "height")
            UserDefaults.standard.set(weightLabel.text!, forKey: "weight")


        }
        else if selectorLabel.selectedSegmentIndex == 1 {
            heightPicker.reloadAllComponents()
            weightPicker.reloadAllComponents()
            heightLabel.text = "Height (cm)"
            weightLabel.text = "Weight (kgs)"
            weightInput.text = "45"
            heightInput.text = "121"
            UserDefaults.standard.set(selectorLabel.selectedSegmentIndex, forKey: "stateSelected")
            UserDefaults.standard.set(heightLabel.text!, forKey: "height")
            UserDefaults.standard.set(weightLabel.text!, forKey: "weight")
        }
    }

Pickerview code:

    var height = (48...96).map(String.init)
    var weight = (100...350).map(String.init)
    var heightCM = (121...244).map(String.init)
    var weightKG = (45...159).map(String.init)

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        var countrows : Int = height.count
        if pickerView == weightPicker && weightLabel.text == "Weight (lbs)" {
        countrows = self.weight.count
        }
        else if pickerView == weightPicker && weightLabel.text == "Weight (kgs)"
        {
        countrows = self.weightKG.count
        }
        return countrows
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if weightLabel.text == "Weight (lbs)" {
            if pickerView == heightPicker {
                let titleRow = height[row]
                return titleRow
            } else if pickerView == weightPicker {
                let titleRow = weight[row]
                return titleRow
            }
            return ""
        } else if weightLabel.text == "Weight (kgs)" {
            if pickerView == heightPicker {
                let titleRow = heightCM[row]
                return titleRow
            }
            else if pickerView == weightPicker {
                let titleRow = weightKG[row]
                return titleRow
            }
            return ""
            }
        return ""
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if pickerView == heightPicker && heightLabel.text == "Height (in)" {
            self.heightInput.text = self.height[row]

        }
        else if pickerView == weightPicker && weightLabel.text == "Weight (lbs)" {
            self.weightInput.text = self.weight[row]

        }
        else if pickerView == heightPicker && heightLabel.text == "Height (cm)" {
            self.heightInput.text = self.heightCM[row]

        }
        if pickerView == weightPicker && weightLabel.text == "Weight (kgs)" {
            self.weightInput.text = self.weightKG[row]

        }
    }

Upvotes: 0

Views: 553

Answers (1)

Samuel Chavez
Samuel Chavez

Reputation: 786

You can try this to reload your components and reset to the first element in the array each time you switch the segmented control:

@IBAction func segmented(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        heightLabel.text = "Height (in)"
        weightLabel.text = "Weight (lbs)"
    } else {
        heightLabel.text = "Height (cm)"
        weightLabel.text = "Weight (kgs)"
    }
    heightPicker.reloadAllComponents()
    weightPicker.reloadAllComponents()
    heightPicker.selectRow(0, inComponent: 0, animated: true)
    weightPicker.selectRow(0, inComponent: 0, animated: true)
}

Upvotes: 1

Related Questions