问题
I am using this method to get suggestions and i need to show them in tableview:
- (void)placeAutocomplete:(NSString *)autoCompleteString {
[self.autoCompleteSuggestionsList removeAllObjects];
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterCity;
[_placesClient autocompleteQuery:(NSString *)autoCompleteString
bounds:nil
filter:filter
callback:^(NSArray *results, NSError *error) {
if (error != nil) {
NSLog(@"Autocomplete error %@", [error localizedDescription]);
return;
}
for (GMSAutocompletePrediction* result in results) {
//NSLog(@"Result '%@' with placeID %@", result.attributedFullText.string, result.placeID);
//NSRange autoCompleteRange = [result.attributedFullText.string rangeOfString:autoCompleteString];
//if (autoCompleteRange.location == 0) {
//NSString *stringNow = [NSString stringWithFormat:@"%@",result.attributedFullText.string];
[self.autoCompleteSuggestionsList addObject:result.attributedFullText.string];
//NSLog(@"test : %@",stringNow);
//NSLog(@"%@",self.autoCompleteSuggestionsList);
//}
}
}];
[self.autocompleteTableView reloadData];
NSLog(@"%@",self.autoCompleteSuggestionsList);
}
but I cannot access the results outside of the autocompleteQuery method
when logged it shows correctly inside the method but not outside , I am using mutable array to access it but i shows correctly inside but not outside.
I don't need suggestions for using any third party autocomplete pod . I am getting the result i just need them to be accessed from the method so that it could be accessible to show the tableview as well
回答1:
You have to reload the data inside the block.
Reason for doing this is simple because block is run in different thread so when it complete the execution it come in callback block with main thread thats why we need to reload table in block.
- (void)placeAutocomplete:(NSString *)autoCompleteString {
[self.autoCompleteSuggestionsList removeAllObjects];
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterCity;
[_placesClient autocompleteQuery:(NSString *)autoCompleteString
bounds:nil
filter:filter
callback:^(NSArray *results, NSError *error) {
if (error != nil) {
NSLog(@"Autocomplete error %@", [error localizedDescription]);
return;
}
for (GMSAutocompletePrediction* result in results) {
[self.autoCompleteSuggestionsList addObject:result.attributedFullText.string];
}
[self.autocompleteTableView reloadData];
}];
NSLog(@"%@",self.autoCompleteSuggestionsList);
}
来源:https://stackoverflow.com/questions/32796361/access-results-from-google-places-api-ios-for-auto-completion