如何使用目标C在ios中使用webservice url实现搜索选项

时间:2016-01-05 12:07:19

标签: ios objective-c uisearchcontroller

我从网络服务获取数据。如果我们通过服务网址传递任何字符串,那么它将根据它返回数据。我已经在tableview中实现并显示数据。现在我想要的是为它实现搜索选项。所以我使用了uisearchcontroller并且这样做。但它运作不正常。我的意思是如果我们发送信件' y'对于Web服务,它将返回从y开始的所有结果。这是我的代码。

- (void)viewDidLoad {

    [super viewDidLoad];
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.scopeButtonTitles = @[NSLocalizedString(@"ScopeButtonCountry", @"Airport"), NSLocalizedString(@"ScopeButtonCapital", @"AirportCode")];
    self.searchController.searchBar.delegate = self;
    //self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    self.mtableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;
    [self.searchController.searchBar sizeToFit];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)getAirports:(NSString *)needeedString
{
    airportList = nil;
    needeedString = [NSString stringWithFormat:@""];
    NSString *apiKey = [NSString stringWithFormat:@"some api"];
    NSString *fullUrl = [NSString stringWithFormat:@"someurl%@%@",apiKey,needeedString];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:fullUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *result = (NSArray *)responseObject;
        airportList = [NSMutableArray array];
        for (NSDictionary *all in result)
        {
            Details *d1 = [Details new];
            d1.airport = [all objectForKey:@"Airport"];
            [airportList addObject:d1];
            [self.mtableView reloadData];

        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = searchController.searchBar.text;
    if (searchString == nil || [searchString  isEqual: @""]) {
        [self getAirports:@""];
        [self.mtableView reloadData];
    }
    [self getAirports:searchString];
    [self.mtableView reloadData];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [airportList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ci"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ci"];
    }

    Details *newDetails = [airportList objectAtIndex:indexPath.row];

    cell.textLabel.text = newDetails.airport;

    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Details *newDetails = [airportList objectAtIndex:indexPath.row];
    NSString *selectedText = newDetails.airport;
    [[NSUserDefaults standardUserDefaults] setObject:selectedText forKey:@"st"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [self dismissViewControllerAnimated:YES completion:nil];
}

3 个答案:

答案 0 :(得分:2)

在调度主队列中调用重新加载:

dispatch_async(dispatch_get_main_queue(), ^{
      [self.mtableView reloadData];
});

答案 1 :(得分:2)

请找到我所做的以下更改,希望它能正常工作

- (void)getAirports:(NSString *)needeedString
    {
        airportList = nil;
        needeedString = [NSString stringWithFormat:@""];
        NSString *apiKey = [NSString stringWithFormat:@"some api"];
        NSString *fullUrl = [NSString stringWithFormat:@"someurl%@%@",apiKey,needeedString];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:fullUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *result = (NSArray *)responseObject;
        airportList = [NSMutableArray array];
        for (NSDictionary *all in result)
        {
            Details *d1 = [Details new];
            d1.airport = [all objectForKey:@"Airport"];
            [airportList addObject:d1];
           //remove reload data method from here because it will reload your table on each iteration

        }
 dispatch_async(dispatch_get_main_queue(), ^{
       //reload on main thread
       [self.mtableView reloadData];
    });

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = searchController.searchBar.text;
    if (searchString != nil && ![searchString  isEqual: @""]) {
       [self getAirports:searchString];
    }
    //here there is no need to reload
   //also changed condition
}

答案 2 :(得分:1)

我猜在方法

- (void)getAirports:(NSString *)needeedString

除去

[self.mtableView reloadData];
在for循环之外的

您可以像

一样调用reloadData
    self.mtableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES