带小数点的PickerView

时间:2018-03-07 19:16:19

标签: swift uipickerview

我使用了带有四个组件的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)

}

我的问题......我想添加一个只有'。'的第五个组件。在它(所以空间均匀分布)。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

你可以尝试

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


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

答案 1 :(得分:1)

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

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

假设.表示小数分隔符,请记住,世界上许多用户不使用.作为小数分隔符。所以你真的应该展示出合适的角色。您可以获得decimalSeparator的{​​{1}}属性。