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 8188577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:57:39+00:00 2026-06-07T02:57:39+00:00

I have a UITextView which is connected to UITableView for Autocompletion. The problem is

  • 0

I have a UITextView which is connected to UITableView for Autocompletion.

The problem is the table display is not properly formatted, i have following problems with this:

  1. the details are not in order(ex: if i press a it is not displaying words starts with a first, it display as it likes).

  2. i have three kind of words in my txt file(like Apple, A pple, A.pple);
    in my table it display only A pple and A.pple but not Apple if i start search with letter ‘Ap’ even it displays A pple till i write ‘A P’ then it stops displaying the words.

Can any one let me know what to do that?

Please find my code for your reference:

Sorry for posting all the code, i am doing because i cannot find were its going wrong!!!

- (void) finishedSearching {
    [usernameField resignFirstResponder];
    autoCompleteTableView.hidden = YES;
}

- (void)viewDidLoad
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"employee.txt" ofType:nil];
    NSData* data = [NSData dataWithContentsOfFile:filePath];
    //Convert the bytes from the file into a string
    NSString* string = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];

    //Split the string around newline characters to create an array
    NSString* delimiter = @"\n";
    NSArray *item = [string componentsSeparatedByString:delimiter];
    elementArray = [[NSMutableArray alloc] initWithArray:item];
    usernameField = [[UITextField alloc] initWithFrame:CGRectMake(204, 405, 264, 31)];
    usernameField.borderStyle = 3; // rounded, recessed rectangle
    usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
    usernameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    usernameField.textAlignment = UITextAlignmentLeft;
    usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    usernameField.returnKeyType = UIReturnKeyDone;
    usernameField.font = [UIFont fontWithName:@"Trebuchet MS" size:20];
    usernameField.textColor = [UIColor blackColor];
    usernameField.placeholder=@"Login id";
    [usernameField setDelegate:self];
    [self.view addSubview:usernameField];

    autoCompleteArray = [[NSMutableArray alloc] init];
    autoCompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(204, 450, 264, tableHeight) style:UITableViewStylePlain];
    autoCompleteTableView.delegate = self;
    autoCompleteTableView.dataSource = self;
    autoCompleteTableView.scrollEnabled = YES;
    autoCompleteTableView.hidden = YES; 
    autoCompleteTableView.rowHeight = tableHeight;
    [self.view addSubview:autoCompleteTableView];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

    // Put anything that starts with this substring into the autoCompleteArray
    // The items in this array is what will show up in the table view

    [autoCompleteArray removeAllObjects];

    for(NSString *curString in elementArray) {
        NSRange substringRangeLowerCase = [curString rangeOfString:[substring lowercaseString]];
        NSRange substringRangeUpperCase = [curString rangeOfString:[substring uppercaseString]];
        if (substringRangeLowerCase.length != 0 || substringRangeUpperCase.length != 0) {
            [autoCompleteArray addObject:curString];
        }
    }
    autoCompleteTableView.hidden = NO;
    [autoCompleteTableView reloadData];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    [super touchesBegan:touches withEvent:event];
    [self finishedSearching];
}

#pragma mark UITextFieldDelegate methods

// Close keyboard when Enter or Done is pressed
- (BOOL)textFieldShouldReturn:(UITextField *)textField {    
    BOOL isDone = YES;

    if (isDone) {
        [self finishedSearching];
        return YES;
    } else {
        return NO;
    }   
} 

// String in Search textfield
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

#pragma mark UITableViewDelegate methods

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {

    //Resize auto complete table based on how many elements will be displayed in the table
    if (autoCompleteArray.count >=3) {
        autoCompleteTableView.frame = CGRectMake(204, 450, 264, tableHeight*3);
        return autoCompleteArray.count;
    }

    else if (autoCompleteArray.count == 2) {
        autoCompleteTableView.frame = CGRectMake(204, 450, 264, tableHeight*2);
        return autoCompleteArray.count;
    }   

    else {
        autoCompleteTableView.frame = CGRectMake(204, 450, 264, tableHeight);
        return autoCompleteArray.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] ;
    }

    cell.textLabel.text = [autoCompleteArray objectAtIndex:indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    usernameField.text = selectedCell.textLabel.text;
    usernameField.text=[usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    NSLog(@"%@",usernameField.text);
    [self finishedSearching];
}



- (void)viewDidUnload
{

    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

Thanks in Advance!!!!

  • 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-07T02:57:40+00:00Added an answer on June 7, 2026 at 2:57 am

    follow this code.. U have done all correctly but searching the element is the problem. U have written the code. if your checking length alone. if its not null it will show data. but u have to compare the first letter with element array. so that it works fine.

    - (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
        [autocompleteArray removeAllObjects];
    
        for(NSString *curString in elementArray) {
            NSRange substringRange = [curString rangeOfString:substring];
            if (substringRange.location == 0) {            
                [autocompleteArray 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 have the following problem: I have a standard UITextView which I resize once
I have a simple UITextView in which I display some links. The UITextView automatically
i have this MainViewController, which is a controller for my table view. Whenever I
I have UITextView, which is left aligned. When last word does not fit on
I currently have a UITextView which is contained in a UIViewController using the following
I am looking for a simple answer for this problem... I have a UITextView
I have a UITextView in which i have 5 hyperlinks and 5 phone numbers.How
I have HTML Content which was being displayed in a UITextView . The next
I have a XIB file with a UITextView, which I'm associating with a UITextView*
I'm writing iPhone/iPad application. I have UITextView in which I automatically appending text. How

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.