NSPredicate模糊搜索和精确搜索

端木兴国
2023-12-01
#pragma mark ------------  searchBar 代理方法  --------------------------

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    /**< 模糊查找*/
    NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", @"name", searchText];
    self.searchResultArray = [NSArray arrayWithArray:[_allCityArray filteredArrayUsingPredicate:predicateString]];
    
    /**< 精确查找*/
    //  NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", @"name", searchText];
    
    [self.searchDisplayController.searchResultsTableView reloadData];

}

 

使用的是 self.searchDisplayController.searchResultsTableView显示搜索的数据;

转载于:https://www.cnblogs.com/cocoajin/p/3417866.html

 类似资料: