当用户在搜索栏中输入时,自动完成搜索结果表来自JSON

时间:2015-07-08 18:13:54

标签: ios objective-c

您好,当用户在搜索栏中输入时,我希望在表格视图中使用Web服务请求显示来自 JSON的数据,为此我正在发送Web服务请求。

我能够在表格中显示数据他在搜索栏中输入iphone并从搜索栏中删除iphone,当用户在搜索栏中删除输入测试时我能够获取表格中的所有数据。但是当用户在搜索栏中输入时无法获得。

我的代码--->

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSMutableString *)searchText{
 NSLog(@"user is typing");
 if ([searchBar.text length] >= 2) 
    {
     mutableString = searchText;
     NSLog(@"\n\n----->%@",mutableString);

    // Call Network Request
    [self performSelector:@selector(sendSearchRequest) withObject:searchText afterDelay:0.2];
      }
    else 
    {
    }
   [self.tableView reloadData];
}

//网络服务请求---->

- (void)sendSearchRequest{
NSString     *weburl = @"http://vcmobileapi.azuresites.net/api/Common/getAutoSuggestion?q=iphone";
NSString * completeRequestUrl = [NSString stringWithFormat:@"%@%@", weburl,mutableString];
DDLogVerbose(@"Complete_URL--->%@",completeRequestUrl);

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = requestSerializer;

[manager GET:completeRequestUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);

if ([responseObject isKindOfClass:[NSDictionary class]] || [responseObject isKindOfClass:[NSMutableDictionary class]]){
    self.airlines = [responseObject valueForKeyPath:@"wordSuggestionList"];
    NSLog(@"\n\n\n\n---->Airlines%@",self.airlines);

};
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
}

JSON响应---->

{
  "Word": "ip",
  "wordSuggestionList": [
     {
        "label": "ipad",
        "type": "product"
     },
     {
        "label": "ipod",
        "type": "product"
     },
     {
         "label": "ipms",
        "type": "product"
     },

//当搜索栏成为第一响应者时调用

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
// Set searchString equal to what's typed into the searchbar
NSString *searchString = self.searchController.searchBar.text;
[self updateFilteredContentForAirlineName:searchString];
// If searchResultsController
if (self.searchController.searchResultsController) {
    UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;
    // Present SearchResultsTableViewController as the topViewController
    SearchResultVC *vc = (SearchResultVC *)navController.topViewController;
    // Update searchResults
    vc.searchResults = self.searchResults;
    // And reload the tableView with the new data
    [vc.tableView reloadData];
}
}

//基于searchString更新self.searchResults,这是传递给此方法的参数

- (void)updateFilteredContentForAirlineName:(NSString *)airlineName{    
if (airlineName == nil) {
    // If empty the search results are the same as the original data
    self.searchResults = [self.airlines mutableCopy];
} else {

    NSMutableArray *searchResults = [[NSMutableArray alloc] init];
    // Else if the airline's name is
    for (NSDictionary *airline in self.airlines) {
        if ([airline[@"label"] containsString:airlineName]) {
            NSString *str = [NSString stringWithFormat:@"%@", airline[@"label"] /*, airline[@"icao"]*/];
            [searchResults addObject:str];
            [self.tableView reloadData];
        }
        self.searchResults = searchResults;

    }
}
}

0 个答案:

没有答案