【问题标题】:contents of UIPickerView depending on the choice of another UIPickerViewUIPickerView 的内容取决于另一个 UIPickerView 的选择
【发布时间】:2012-01-10 19:48:26
【问题描述】:

我有两个 UIPickerView。第一个包含世界各国。当用户选择一个国家时,我联系一个网络服务来检索这个国家的城市。一切对我来说都很好,但只有当我第一次选择这个国家时。但是当我第二次选择国家时,第一次显示的城市仍然存在。这是我的代码。看起来 UIPickerView 的内容无法更改。

- (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@"cities on urbanArray ... on resultCities 会显示新选择国家的新城市,所以问题不存在

【问题讨论】:

  • 你在哪里重新加载pickerview?
  • @Trisha 我必须重新加载它?
  • 我在 resultCities 中添加了 [pickerView reloadAllComponents],效果很好,谢谢

标签: iphone objective-c xcode uipickerview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多