2

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

4

4 回答 4

2

好的,你有两个选择器,比如说 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,Hari

于 2012-01-19T11:57:33.547 回答
1

它们是分开的选择器视图,还是一个带有两列的选择器视图?

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

于 2012-01-19T11:02:19.537 回答
0

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

于 2012-01-19T11:02:18.893 回答
0
  NSUInteger selectedRow= [CountryPicker selectedRowInComponent:0];

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

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

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

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

在区域选择器上

于 2012-01-19T11:05:50.790 回答