尝试重新加载所有数据,使pickerview无法正常运行

时间:2019-06-18 14:59:32

标签: swift xcode uipickerview

我开发了一个应用程序,其中有两个标签,两个pickerviews和一个分段的控制按钮,可在Imperial和Metric之间更改标签,并基于该标签,将在每个pickerview中加载不同的数组。目前,这一切正常。我遇到的问题是,由于每个pickerview都是一个体重和身高的数组,当用户按下分段的控制按钮以更改测量类型时,它将更改第二个数组的索引值。例如,如果我当前处于英制范围内,并且我选择了105(从100开始),如果我转换为公制,它将添加正确的数组,但是它会自动将我的起始数字增加5(索引之间的差值)数字和我从pickerview中选择的数字,反之亦然。

这是我的选择器代码:

@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代码:

    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]

        }
    }

1 个答案:

答案 0 :(得分:1)

每次切换分段控件时,您都可以尝试重新加载组件并重置为数组中的第一个元素:

@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)
}