0

I am trying to use a search bar for input, but I want to ensure the input is in a certain format. So based on a list I need the user to start typing and have suggestions pop up that can be clicked on. Similar to many flight tracking apps that recommend airports that you can tap on and add.

1 Answers1

0

This method from UISearchBarDelegate will be useful.

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

  NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"term BEGINSWITH[cd] %@", searchText];
  [self.searchResults filterUsingPredicate:searchPredicate];

  [self.tableView reloadData];
}

If there are too many results to load all of them into memory, consider forcing the user to enter at least 1 character before loading

  if ([searchText length] && [searchText length] < 2) {
    // Load answers matching search text. Only useful for BEGINSWITH predicate
    return;
  }

Replace BEGINSWITH with MATCHES or CONTAINS if you want to match within an answer

Audun Kjelstrup
  • 1,430
  • 8
  • 13