如何在 UIPickerView 中选择行

发布于 2024-10-27 19:57:56 字数 367 浏览 3 评论 0原文

我有一个 UIPickerView 有两个组件。组件 1 中的行数取决于组件 0。我正在尝试以下操作:

// [picker reloadAllComponents];
[picker selectRow:5 inComponent:0 animated:NO];
[picker reloadComponent:1]; // [picker reloadAllComponents];
[picker selectRow:3 inComponent:1 animated:NO];
// [picker reloadAllComponents];

发生的情况是组件 0 设置为索引 5,而组件 1 显示信息,就好像在组件 0 中选择了索引 0 一样!

I have a UIPickerView that has two components. The number of rows in component 1 depends on component 0. I am trying the following:

// [picker reloadAllComponents];
[picker selectRow:5 inComponent:0 animated:NO];
[picker reloadComponent:1]; // [picker reloadAllComponents];
[picker selectRow:3 inComponent:1 animated:NO];
// [picker reloadAllComponents];

What happens is that component 0 is set to index 5 and component 1 is showing the info as if index 0 was selected in component 0!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2024-11-03 19:57:56

我尝试了完全相同的事情,效果非常好。以下是一些相关代码:

此代码仅在您手动选择行时才相关:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        [pickerView reloadComponent:1];
    }
}

我添加了一个按钮,在点击该按钮时执行您的代码。

- (void)buttonAction:(id)sender {
    [pickerView selectRow:3 inComponent:0 animated:NO];
    [pickerView reloadComponent:1];
    [pickerView selectRow:1 inComponent:1 animated:NO];
}

这是我拥有的最后一段相关代码:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return 5;
            break;
        case 1:
            return [pickerView selectedRowInComponent:0];
            break;
        default:
            break;
    }
    return 5;
}

一切都按预期工作,因此您发布的行不是导致问题的行。如果您在阅读我的代码(经过测试且有效)后仍无法弄清楚,我建议您发布更多代码。

I tried the exact same thing and it works perfectly. Here's some relevant code:

This code is only relevant if you select the row manually:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        [pickerView reloadComponent:1];
    }
}

I added a button that executes your code when it's tapped.

- (void)buttonAction:(id)sender {
    [pickerView selectRow:3 inComponent:0 animated:NO];
    [pickerView reloadComponent:1];
    [pickerView selectRow:1 inComponent:1 animated:NO];
}

Here's the last relevant piece of code that I have:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return 5;
            break;
        case 1:
            return [pickerView selectedRowInComponent:0];
            break;
        default:
            break;
    }
    return 5;
}

Everything works as expected, so the lines you posted aren't the ones causing the problem. If you can't figure it out after having read my code (that is tested and working) I suggest you post some more code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文