0

The following code will generate a custom autocomplete Table View autocompleteTableView below my text field txtActivity. How do I hide the Table View when text field is empty.

- (void)viewDidLoad
{    
     [super viewDidLoad];
     self.txtActivity.delegate = self;
     [self cofigureAutoComTableView];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString *)string 
{
    autocompleteTableView.hidden = NO;

    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

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

    autocompleteMArray = [[NSMutableArray alloc] init];

    [autocompleteMArray removeAllObjects];

    for(NSString *curString in sCategory) { //**** sCategory is a mutable array generated in another method not shown ****

    NSRange substringRange = [curString rangeOfString:substring];

    if (substringRange.length > 0) {

        [autocompleteMArray addObject:curString];
    }

}
  NSLog(@"*******The autocompleteMArray : %@ ", autocompleteMArray);
  [autocompleteTableView reloadData];
}


-(void)cofigureAutoComTableView
{
     autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.txtActivity.frame.origin.x-10,self.txtActivity.frame.origin.y+32,self.txtActivity.frame.size.width, 120) style:UITableViewStylePlain];

     autocompleteTableView.delegate = self;
     autocompleteTableView.dataSource = self; //**** Setting this to self, is it correct???
     autocompleteTableView.scrollEnabled = YES;
     autocompleteTableView.hidden = YES;
     [self.view addSubview:autocompleteTableView];

}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell = [self.autocompleteTableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

     cell.textLabel.text =  [autocompleteMArray objectAtIndex:indexPath.row];

    return cell;
  }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath (NSIndexPath *)indexPath;
{
    //**** NOT DETECTING *******
    NSLog(@"Selected Cell %@", [autocompleteMArray objectAtIndex:indexPath.row]);
}
Hanz Cheah
  • 761
  • 5
  • 15
  • 44

2 Answers2

0

If substringRange length is 0 then assign sCategory array to autocompleteMArray and reload the tableview.

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
 {
    autocompleteMArray = [[NSMutableArray alloc] init];
    [autocompleteMArray removeAllObjects];
    if ([substring length] == 0)
    {
      for(NSString *curString in sCategory)
      { //**** sCategory is a mutable array generated in another method not shown ****
        NSRange substringRange = [curString rangeOfString:substring];
        if (substringRange.length > 0)
        {
          [autocompleteMArray addObject:curString];
        }
      }
    }
    else
    {
      autocompleteMArray = sCategory;
    }
    NSLog(@"*******The autocompleteMArray : %@ ", autocompleteMArray);
    [autocompleteTableView reloadData];
 }
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
0
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([[NSString stringWithFormat:@"%@%@",textField.text,string] isEqualToString:@""]) {
    autocompleteTableView.hidden = YES;
} else {
    autocompleteTableView.hidden = range.location == 0 ? YES : NO;
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
}
return YES; }
  • Hi Rohit, this is slightly working but the only thing is only the 2nd input of substring then only the autocomplete Table View will appear. I believe it is range, because when first input the substring, range is always not zero from last? Please help. – Hanz Cheah Mar 16 '18 at 08:42
  • I modified my answer so please try again and let me know it is working fine or not. – Rohit Kardani Mar 16 '18 at 09:01
  • @HansheungCheah still you facing any problem or this code work – Rohit Kardani Mar 18 '18 at 09:54