gpt4 book ai didi

swift - 带小数点的 PickerView

转载 作者:行者123 更新时间:2023-11-28 12:07:14 25 4
gpt4 key购买 nike

我使用了一个带有四个组件的 UIPickerView 来在没有键盘的情况下输入数字。我添加了一个小数点,只有一个“。”在 UILabelView 中。查看屏幕了解想法。

UIPickerView

我的 UIPickerView 代码是:

func numberOfComponents(in weightPickerView: UIPickerView) -> Int {
return 4
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return loopingMargin * numbers.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return numbers[row % numbers.count]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let currentIndex = row % numbers.count
digits[component] = currentIndex
let weightString = "\(digits[0])\(digits[1]).\(digits[2])\(digits[3])"
weightField.text = weightString
}

在 viewDidLoad() 里面的部分代码是:

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

weightPickerView.dataSource = self
weightPickerView.delegate = self
weightPickerView.selectRow((loopingMargin / 2) * numbers.count, inComponent: 0, animated: false)
weightPickerView.selectRow((loopingMargin / 2) * numbers.count, inComponent: 1, animated: false)
weightPickerView.selectRow((loopingMargin / 2) * numbers.count, inComponent: 2, animated: false)
weightPickerView.selectRow((loopingMargin / 2) * numbers.count, inComponent: 3, animated: false)

}

我的问题...我想添加第五个组件,其中只有一个“.”。在其中(因此空间分布均匀)。我该怎么做?

最佳答案

您只需要更新您的选择器 View 方法来提供额外的组件。假设你想要它在中间,你的代码需要是:

func numberOfComponents(in weightPickerView: UIPickerView) -> Int {
return 5
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return component == 2 ? 1 : loopingMargin * numbers.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return component == 2 ? "." : numbers[row % numbers.count]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component != 2 {
var index = component
if component > 2 {
index -= 1
}
let currentIndex = row % numbers.count
digits[index] = currentIndex
let weightString = "\(digits[0])\(digits[1]).\(digits[2])\(digits[3])"
weightField.text = weightString
}
}

override func viewDidLoad() {
super.viewDidLoad()

weightPickerView.dataSource = self
weightPickerView.delegate = self
weightPickerView.selectRow((loopingMargin / 2) * numbers.count, inComponent: 0, animated: false)
weightPickerView.selectRow((loopingMargin / 2) * numbers.count, inComponent: 1, animated: false)
weightPickerView.selectRow((loopingMargin / 2) * numbers.count, inComponent: 3, animated: false)
weightPickerView.selectRow((loopingMargin / 2) * numbers.count, inComponent: 4, animated: false)
}

假设 . 代表小数点分隔符,请记住,世界各地的许多用户不使用 . 作为小数点分隔符。所以你真的应该表现出合适的性格。您可以获得 NumberFormatterdecimalSeparator 属性。

关于swift - 带小数点的 PickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49159646/

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