UIPickerView selectRow 不起作用 [英] UIPickerView selectRow doesn't works

查看:27
本文介绍了UIPickerView selectRow 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的 PickerView 中选择一行,但它不起作用.它停留在第一行.

I try to select a row in my PickerView but it doesn't works. It stays at the first row.

这是我的代码:

@IBOutlet weak var pickerView: UIDatePicker!

func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int
{
    return 1
}

func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int
{
    return 10
}

func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String!{
    return String(row + 1)
}


//In viewDidLoad
pickerView.selectRow(6, inComponent: 0, animated: true)

pickerView 运行良好,显示 1 到 10,但 selectRow 不起作用.

The pickerView is working well it shows 1 to 10 but the selectRow doesn't works.

它应该选择第六行,但它留在第一行.

It should select the sixth row but it stays on the first one.

推荐答案

虽然在 viewDidLoad 方法中,你的选择器是空的.由于没有第 6 个元素可供选择,它不会选择它.

While inside the viewDidLoad method, your picker is empty. Since there is no 6th element to pick, it doesn't pick it.

如果您将 select 调用的执行延迟到数据加载完成后,您可以执行此操作.大概一个周期就够了.

You can do this if you delay execution of the select call until after the data is finished loading. Likely one cycle will be enough.

首先,尝试将您的选择移动到 viewDidAppear.

First though, try moving your select to viewDidAppear.

祝你好运!

这篇关于UIPickerView selectRow 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆