UIPickerView 的内容取决于另一个 UIPickerView 的选择
我有两个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
put - (void)onLocationSelection
{
}再次编码。
put - (void)onLocationSelection
{
}code again.