【问题标题】:How to detect values from UIPickerView while is rolling?滚动时如何检测 UIPickerView 中的值?
【发布时间】:2018-08-30 21:56:55
【问题描述】:

我正在尝试在 UIPickerView 滚动时读取数据。

Didselectedrow 仅在 UIPicker 不移动时为您提供后行值。滚动时如何从 UIPickerView 中获取值?

我需要准确地知道中间行上传递的数据,获取它们并将其他对象相应地更新到 UIView 中。

我尝试使用计时器调用读取函数,但是在滚动时 didselectedrow 不会更新自己,请参见下面的代码:

// define the timer
let cycleTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.readUIPicker), userInfo: nil, repeats: true)

由于定义了计时器,它每 0.1 秒调用一次读取函数

// read UIPicker
func readUIPicker() {
    let  selectedRow = myPicker.selectedRow(inComponent: 0)

    // Do something here based on selectedRow value
    print("SelectedRow = \(selectedRow)")
}

问题是 selectedRow 只会在滚动停止后更新。无法控制传递的元素。

【问题讨论】:

标签: swift uipickerview


【解决方案1】:

虽然没有 API 在滚动时获取选取器的值,但选取器视图将调用您的数据源和委托方法来获取给定行的标题或视图,以便在用户滚动选取器视图时显示它.因此,您能做的最好的事情就是利用选择器视图只是询问某行的标题或视图这一事实。但该行不一定是当前的中心行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    相关资源
    最近更新 更多