UIPickerView 的内容取决于另一个 UIPickerView 的选择

发布于 2024-12-20 07:11:14 字数 2446 浏览 3 评论 0原文

我有两个 UIPickerView。第一个包含世界各国。当用户选择一个国家/地区时,我会联系网络服务来检索该国家/地区的城市。一切对我来说都很好,但只有当我第一次选择这个国家时。但当我第二次选择国家/地区时,第一次显示的相同城市仍然存在。这是我的代码。看来 UIPickerView 的内容不能被 chnage 。

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
    NSString *codeCity;
    codeCity=[countriesArray objectAtIndex:row];

    if([pickerView tag] == 1)
    {
        NSString *codeCity;
    codeCity=[countriesArray objectAtIndex:row];

        return [countriesArray objectAtIndex:row];
    }
    else 

    return [citiesArray objectAtIndex:row];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
    if([pickerView tag] == 1)
        return [countriesCodeArray count];
    else
        return [citiesArray count];

}

- (void)onLocationSelection {

    NSInteger row = [self.countrys selectedRowInComponent:0];
    if(([countriesArray count])!=0)
    {
        if ( [countryField isFirstResponder] ) 
        {

            choosedCodeCity=[countriesCodeArray objectAtIndex:row];
            choosedNameCity=[countriesArray objectAtIndex:row];
            countryField.text = choosedNameCity;
            [countryField resignFirstResponder];
            [self getCities];
            NSLog(@"get cities() %@",choosedCodeCity);
        }

-(void)getCities
{
    NSString * myURLString = [NSString stringWithFormat:@"http://localhost:8080/Data/resources/converter.city/CountryCode/%@",choosedCodeCity];

    NSURL *url =[NSURL URLWithString:myURLString];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    request.didFinishSelector = @selector(resultCities:);
    request.didFailSelector = @selector(resultCitiesError:);

    [request startAsynchronous];


}


-(void)resultCities :(ASIHTTPRequest *)request
{
    [citiesArray release];
    citiesArray= [[NSMutableArray alloc] init];

    NSString *responseString = [request responseString];
    NSDictionary *json    = [responseString JSONValue];
   // NSLog(@"%@",json);

    for (int i=0; i<[json count]; i++)
    {



            [citiesArray addObject:[[json objectAtIndex:i] objectForKey:@"name"]];



   NSLog(@"cities on citiesArray %@ ", citiesArray);

        }
    }

每次我更改县时,NSLog@“城市数组上的城市...结果城市向我显示新选定国家/地区的新城市,所以问题不存在

I have two UIPickerView. The first contains the world coutries. When the user selects a country, I contact a web service to retrieve the cities of this country. All work fine for me but only when I choose the country for the first time. But when I choose the country a second time, the same cities that have displayed the first time are there . this is my code . it looks like the contents of UIPickerView can not be chnage .

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
    NSString *codeCity;
    codeCity=[countriesArray objectAtIndex:row];

    if([pickerView tag] == 1)
    {
        NSString *codeCity;
    codeCity=[countriesArray objectAtIndex:row];

        return [countriesArray objectAtIndex:row];
    }
    else 

    return [citiesArray objectAtIndex:row];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
    if([pickerView tag] == 1)
        return [countriesCodeArray count];
    else
        return [citiesArray count];

}

- (void)onLocationSelection {

    NSInteger row = [self.countrys selectedRowInComponent:0];
    if(([countriesArray count])!=0)
    {
        if ( [countryField isFirstResponder] ) 
        {

            choosedCodeCity=[countriesCodeArray objectAtIndex:row];
            choosedNameCity=[countriesArray objectAtIndex:row];
            countryField.text = choosedNameCity;
            [countryField resignFirstResponder];
            [self getCities];
            NSLog(@"get cities() %@",choosedCodeCity);
        }

-(void)getCities
{
    NSString * myURLString = [NSString stringWithFormat:@"http://localhost:8080/Data/resources/converter.city/CountryCode/%@",choosedCodeCity];

    NSURL *url =[NSURL URLWithString:myURLString];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    request.didFinishSelector = @selector(resultCities:);
    request.didFailSelector = @selector(resultCitiesError:);

    [request startAsynchronous];


}


-(void)resultCities :(ASIHTTPRequest *)request
{
    [citiesArray release];
    citiesArray= [[NSMutableArray alloc] init];

    NSString *responseString = [request responseString];
    NSDictionary *json    = [responseString JSONValue];
   // NSLog(@"%@",json);

    for (int i=0; i<[json count]; i++)
    {



            [citiesArray addObject:[[json objectAtIndex:i] objectForKey:@"name"]];



   NSLog(@"cities on citiesArray %@ ", citiesArray);

        }
    }

every time I change county, NSLog@"cities on citiesArray ... on resultCities show me the new cities of the new selected country , so the problem is not there

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

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

发布评论

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

评论(1

弱骨蛰伏 2024-12-27 07:11:14

put - (void)onLocationSelection
{
}再次编码。

put - (void)onLocationSelection
{
}code again.

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