Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:18:43+00:00 2026-06-17T23:18:43+00:00

I have a UITableView that displays results from an API. The API is called

  • 0

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.

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.

SearchViewController.h

#import <UIKit/UIKit.h>

@interface SearchViewController : UIViewController <UITextFieldDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate>{
    UITableView *searchTableView;
    UISearchBar *sBar;
    UISearchDisplayController *searchDisplayController;
}

@property (strong, nonatomic) NSArray *loadedSearches;

@end

SearchViewController.m

#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&client_id=xxx&client_secret=yyy&v=20121223&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

If my problem isn’t clear, let me know.

Feel free to critique other aspects of the code, however I really would appreciate the solution to my problem:) Thanks in advance.

Example API response – http://pastebin.com/UZ1H2Zwy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:18:44+00:00Added an answer on June 17, 2026 at 11:18 pm

    The problem seems to be that you are refreshing the table before you get the data as you are making an asynchronous operation with AFJSONRequestOperation. So your model is probably getting updated correctly but your tableview is one refresh behind. Try moving [searchTableView reloadData] inside the block success callback:

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
          success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
          {
              self.loadedSearches = JSON[@"response"][@"venues"];
    
              // refreshing the TableView when the block gets the response
              [searchTableView reloadData];
          }
          failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
          {
              NSLog(@"%@", error.localizedDescription);
          }];
    

    Hope this works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView that displays various nsstrings from a custom object called a
I have a UITableView that pushes, via a storyboard segue, a view, which displays
i have a UITableView, that displays expenses from a current month (see screenshot): My
I have a UITableView that displays the value of a property called name of
I have a UITableView that displays single large images in each cell. The names
I have a UITableView that displays some data that is read in at runtime
I have a UITableView that lists comments from various users. I save all of
I have a UITableView that displays a subset of a large number of entities
I have a RSS parser that displays the articles in a UITableView. I also
I have a UITableViewCell that displays person's name from Address Book. When that cell

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.