首页 > 解决方案 > 多组件 UIPickerView 上的默认行

问题描述

该应用程序根据用户在 UIPickerView 上输入的当前里程表值来跟踪给定汽车的行驶距离。我已经能够构造它,有 6 个组件,行值范围从 0 到 9。如图所示:

PickerView 演示

当 PickerView 出现时,我希望它能够将最后跟踪的距离显示为选择的默认行。最后跟踪的距离存储在 Int 类型的 var (lastDistance) 中。

我能够根据 Int 的自定义扩展遍历距离上的每个数字。(lastDistance.digits:[Int])。还循环遍历每个组件。但不知道在 lastDistance 中为数字选择正确组件的正确方法。

组件数组始终为: components=[0, 1, 2, 3, 4, 5]

例如,如果值为 4400 英里。数字数组是 [4, 4, 0, 0],这个距离的正确选择应该是:

pickerView.selectRow(lastDistance.digits[0], inComponent: components[2], animated: false)
pickerView.selectRow(lastDistance.digits[1], inComponent: components[3], animated: false)
pickerView.selectRow(lastDistance.digits[2], inComponent: components[4], animated: false)
pickerView.selectRow(lastDistance.digits[3], inComponent: components[5], animated: false)

问题是当距离从 2 到 6 位变化时,选择的组件应该改变。

标签: iosswiftalgorithmfunction

解决方案


推荐阅读