Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling an API with UISearchBar
    primarykey
    data
    text
    <p>I have a UITableView that displays results from an API. The API is called whenever the user types into the UISearchBar via searchBar:textDidChange:. Effectively implementing an autocomplete search. My problem is the results loaded into the UITableView seem to be an iteration behind the last API call. </p> <p>Example: User types "union" into the UISearchBar, however no results are shown in the UITableView. User types any character after "union", "unions" for example, and the API results from "union" are displayed in the UITableView. When user scrolls down through results (of "unions", but really "union") "repopulated cells" display "unions" result.</p> <p>SearchViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface SearchViewController : UIViewController &lt;UITextFieldDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate&gt;{ UITableView *searchTableView; UISearchBar *sBar; UISearchDisplayController *searchDisplayController; } @property (strong, nonatomic) NSArray *loadedSearches; @end </code></pre> <p>SearchViewController.m</p> <pre><code>#import "SearchViewController.h" #import "AFJSONRequestOperation.h" @interface SearchViewController () @end @implementation SearchViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"Search"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; searchTableView = [[UITableView alloc] initWithFrame:self.view.bounds]; searchTableView.delegate = self; searchTableView.dataSource = self; [self.view addSubview:searchTableView]; sBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)]; sBar.placeholder = @"Bus Route to..."; sBar.delegate = self; searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:sBar contentsController:self]; searchDisplayController.delegate = self; searchDisplayController.searchResultsDataSource = searchTableView.dataSource; searchDisplayController.searchResultsDelegate = searchTableView.delegate; searchTableView.tableHeaderView = sBar; } -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSString *searchQuery = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=40.4263,-86.9177&amp;client_id=xxx&amp;client_secret=yyy&amp;v=20121223&amp;query='%@'",searchText]; searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [[NSURL alloc] initWithString:searchQuery]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){ self.loadedSearches = JSON[@"response"][@"venues"]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){ NSLog(@"%@", error.localizedDescription); }]; [operation start]; [searchTableView reloadData]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.loadedSearches.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } cell.textLabel.text = self.loadedSearches[indexPath.row][@"name"]; return cell; } @end </code></pre> <p>If my problem isn't clear, let me know.</p> <p>Feel free to critique other aspects of the code, however I really would appreciate the solution to my problem:) Thanks in advance.</p> <p>Example API response - <a href="http://pastebin.com/UZ1H2Zwy" rel="nofollow">http://pastebin.com/UZ1H2Zwy</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload