【问题标题】:Access results from google places api ios for auto completion从 google places api ios 访问结果以自动完成
【发布时间】:2015-09-26 10:52:00
【问题描述】:

我正在使用这种方法来获取建议,我需要在 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);
}

但我无法访问 autocompleteQuery 方法之外的结果

登录时,它在方法内部正确显示,但在外部不正确, 我正在使用可变数组来访问它,但我在内部正确显示但在外部显示不正确。

我不需要关于使用任何第三方自动完成 pod 的建议。 我得到了结果,我只需要从方法中访问它们,以便也可以访问它以显示 tableview

【问题讨论】:

  • 在 autoComplete 块内重新加载表。(完成 for 循环后)
  • 谢谢哥们,非常感谢:
  • 谢谢谢谢,请把这个贴出来作为其他人的答案

标签: ios objective-c uitableview google-places-api


【解决方案1】:

你必须重新加载块内的数据。

这样做的原因很简单,因为块在不同的线程中运行,所以当它完成执行时,它会与主线程一起进入回调块,这就是我们需要在块中重新加载表的原因。

- (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);
}

【讨论】:

  • 快乐编码@Saurabh
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 2020-06-30
相关资源
最近更新 更多