gpt4 book ai didi

swift - 尝试重新加载 pickerview 的所有数据不起作用

转载 作者:行者123 更新时间:2023-11-28 13:35:43 25 4
gpt4 key购买 nike

我开发了一个应用程序,其中有两个标签、两个选择器 View 和一个分段控制按钮,用于在英制和公制之间更改标签,并根据该标签在每个选择器 View 中加载不同的数组。目前这一切都有效。我遇到的问题是因为每个 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")
}
}

选择器 View 代码:

    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]

}
}

最佳答案

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

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

关于swift - 尝试重新加载 pickerview 的所有数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56651990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com