iOS Realizes Fuzzy Search Function

  • 2021-07-26 08:58:07
  • OfStack

Fuzzy search method is to compare the text in the search box with the resources given by the background when the search box begins to edit, including the display of the search text in tableview.

The key parts of the code are as follows:


-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
self.result = nil; 
for (int i = 0; i < self.nameArray.count; i++) { 
NSString *string = self.nameArray[i]; 
if (string.length >= searchText.length) { 
if([self.nameArray[i] rangeOfString:searchText].location !=NSNotFound) 
{ 
[self.result addObject:self.nameArray[i]]; 
} 
} 
} 
[self.tableView reloadData]; 
} 

Related articles: