如何根据另一个选择器的选择更改选择器行?

发布于 2024-12-28 07:58:49 字数 110 浏览 5 评论 0原文

我有两个选择器 - 国家和地区。我需要根据国家/地区选择器中选择的行填充区域选择器。我有数组来填充两个选择器。我需要一些帮助来根据国家/地区选择器的选择更改区域选择器的行。任何帮助将不胜感激。预先非常感谢。

I have two pickers - country and region. I need to populate the region picker based on the row selected in the country picker. I have arrays to populate both the pickers. I need some help to change the rows of region picker based on the country picker's selection. Any help would be greatly appreciated. Thanks a lot in advance.

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

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

发布评论

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

评论(4

陪我终i 2025-01-04 07:58:49

好的,您有两个选择器,例如“countryPicker”和“regionPicker”。

在 UIPickerView 的委托方法中添加一个条件

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{ 
    NSString *pickerTitle = @"";
    if (pickerView == countryPicker)
    {
        pickerTitle =  [countryFeeds objectAtindex:row];
        //assigns the country title if pickerView is countryPicker
    }
    else if (pickerView == regionPicker)
    {
        pickerTitle =  [regionFeeds objectAtindex:row];
        //assigns the region title if pickerView is regionPicker
    }

    return pickerTitle;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == countryPicker)
    {
        //selects the region corresponding to the selected country.
        //totalRegions is a NSDictionary having country names as keys and array of their regions as values
        regionFeeds = [totalRegions objectForKey:[countryFeeds objectAtindex:row]];

        //Now reloading the regionPicker with new values.
        [regionPicker reloadAllComponents];
    }
    else if (pickerView == regionPicker)
    {
        // your code to select a region
    }
}

我希望这可以解决您的问题:)
BR,哈里

Ok You have two pickers lets say countryPicker and regionPicker.

in the delegate method for UIPickerView add one condition

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{ 
    NSString *pickerTitle = @"";
    if (pickerView == countryPicker)
    {
        pickerTitle =  [countryFeeds objectAtindex:row];
        //assigns the country title if pickerView is countryPicker
    }
    else if (pickerView == regionPicker)
    {
        pickerTitle =  [regionFeeds objectAtindex:row];
        //assigns the region title if pickerView is regionPicker
    }

    return pickerTitle;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == countryPicker)
    {
        //selects the region corresponding to the selected country.
        //totalRegions is a NSDictionary having country names as keys and array of their regions as values
        regionFeeds = [totalRegions objectForKey:[countryFeeds objectAtindex:row]];

        //Now reloading the regionPicker with new values.
        [regionPicker reloadAllComponents];
    }
    else if (pickerView == regionPicker)
    {
        // your code to select a region
    }
}

I hope this solves your problem :)
BR, Hari

静谧 2025-01-04 07:58:49

它们是单独的选择器视图,还是具有两列的一个选择器视图?

无论哪种方式,当您更改一个选择器中的值时,只需在另一个选择器上调用 reloadAllComponents (或 reloadComponent:如果您使用的是拆分选择器),然后当它从其数据源重新加载数据时,您可以使用第一个选择器中的值选择器提供正确的区域列表。

Are they separate picker views, or one picker view with two columns?

Either way, when you change the value in one picker, just call reloadAllComponents on the other picker (or reloadComponent: if you are using a split picker) and then when it reloads the data from it's data source you can use the value from the first picker to supply the correct region list.

一瞬间的火花 2025-01-04 07:58:49

我的建议是,请在一个具有两个组件(国家、地区)的选择器中维护国家和地区。如果您这样做,那么我们可以根据另一个组件值更改一个组件值。

My suggestion is please maintain the country and region in one picker with two components(country,region).If u do like this then we can change the one component values based on another component value.

星星的轨迹 2025-01-04 07:58:49
  NSUInteger selectedRow= [CountryPicker selectedRowInComponent:0];

    NSString *userchoice =[self pickerView:CountryPicker titleForRow:selectedRow forComponent:0];

这将为您提供用户在国家/地区选择器中所选行的标题,

然后使用您必须填充区域名称的数组
使用

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

您可以在 RegionPicker 上

  NSUInteger selectedRow= [CountryPicker selectedRowInComponent:0];

    NSString *userchoice =[self pickerView:CountryPicker titleForRow:selectedRow forComponent:0];

This will give you the title for the selected row by the user in country picker

then using the arrays you have to populate names of regions
You can use

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

on the RegionPicker

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