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

  • SEARCH
  • Home
  • 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 8839701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:17:50+00:00 2026-06-14T10:17:50+00:00

I m fairly new to objective-C and implementing the concept of autocomplete feature for

  • 0

I m fairly new to objective-C and implementing the concept of autocomplete feature for a UItextField.I can do it appropriately .but when I select a particular cell then that cell’s text should be displayed in UITextField and correspondingly tableView must be hidden.But Im unable to hide a UITableView after selecting a cell..Where I m going wrong?

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {


    NSURL *urlString= [NSString stringWithFormat:@"http://179.87.89.90/services/Service.svc/GetCities/?p=%@&k=%@",substring,txtId.text];     
    NSURL *jsonUrl =[NSURL URLWithString:urlString];
    NSString *jsonStr = [[NSString alloc] initWithContentsOfURL:jsonUrl];  
    parser = [[NSXMLParser alloc] initWithContentsOfURL:jsonUrl];
    currentHTMLElement=@"3";
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
    [parser release];
    NSLog(@"%@",arr2);



    if([arr2 count]!=0)
    {
        self.autocompleteUrls = [[NSMutableArray alloc] init];

        autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(88, 447, 200, 120) style:UITableViewStyleGrouped];
        autocompleteTableView.delegate = self;
        autocompleteTableView.dataSource = self;
        autocompleteTableView.scrollEnabled = YES;
        // autocompleteTableView.hidden = YES;  
        [self.view addSubview:autocompleteTableView];
        [autocompleteUrls removeAllObjects];
            for(int i=0;i<[arr2 count];i++)
            {
                NSString *curString = [[arr2 objectAtIndex:i] valueForKey:@"Name"];

                NSRange substringRange = [curString rangeOfString:substring];

                if (substringRange.location == 0) 
                    [autocompleteUrls addObject:curString];  

            }
      [autocompleteTableView reloadData];
    }
    else
    {
        autocompleteTableView.delegate=nil;
        autocompleteTableView.dataSource=nil;
        autocompleteTableView.hidden = YES;  

    }

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {



    if( textField == txtcity)
    {
     autocompleteTableView.hidden = NO;
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return autocompleteUrls.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
    }

    cell.textLabel.text = [autocompleteUrls objectAtIndex:indexPath.row];
    cell.textLabel.font=[UIFont boldSystemFontOfSize:12];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    txtcity.text = selectedCell.textLabel.text;
    [autocompleteUrls removeAllObjects];
    [self.autocompleteTableView setHidden:YES];    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        txtcity.text = selectedCell.textLabel.text;
        [autocompleteUrls removeAllObjects];
        autocompleteTableView.hidden=YES;

    }

How can I hide autocompleteTableView after selecting a row ?
Any help would be appreciated..

  • 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-14T10:17:51+00:00Added an answer on June 14, 2026 at 10:17 am

    The issue is with the if condition, when the if is evaluated true then again the autocompleteTableView is again allocated and added to self.view. It’ll be placed over the previous tableView and youare losing the reference of previous tableView. If you call autocompleteTableView.hidden = YES. Last added tableView will be hidden. But previously added tableViews will be there.

    Just change the if block like:

    if([arr2 count]!=0)
    {
        self.autocompleteUrls = [[NSMutableArray alloc] init];
        if(autocompleteTableView)
             [autocompleteTableView removeFromSuperView];
        autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(88, 447, 200, 120) style:UITableViewStyleGrouped];
        autocompleteTableView.delegate = self;
        autocompleteTableView.dataSource = self;
        autocompleteTableView.scrollEnabled = YES;
        // autocompleteTableView.hidden = YES;  
        [self.view addSubview:autocompleteTableView];
            for(int i=0;i<[arr2 count];i++)
            {
                NSString *curString = [[arr2 objectAtIndex:i] valueForKey:@"Name"];
    
                NSRange substringRange = [curString rangeOfString:substring];
    
                if (substringRange.location == 0) 
                    [autocompleteUrls addObject:curString];  
    
            }
      [autocompleteTableView reloadData];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to Objective C and iOS programming but am constantly trying
I'm fairly new to Objective-C and I can't figure out how to wait in
I am fairly new to Objective-C, but haven't been able to find the answer
I'm fairly new to Objective C, but have had some experience with Java. In
I'm fairly new to objective c but I'm using this code NSURL *url =
Fairly new to Objective C and trying to work within Storyboards given it's the
I am fairly new to objective-c and am having trouble understanding the warning message
I am fairly new to Objective-C and whilst running the Clang static analyser this
I'm fairly new to objective-c, and am looking to pass a number of key-value
I'm fairly new to Objective-C and cocoa programming, so I don't really understand the

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.