1

在用户将搜索结果键入 UISearchBar 后,我的应用程序正在解析搜索结果。一切正常。但现在我想搜索全文。结果在我的 NSMutableArray 中,但未显示。我认为问题在于这段代码:

NSRange nameRange = [SDPE.title rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch];

我能做些什么?

4

2 回答 2

1

检查这个:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
  searchBar.showsCancelButton = YES;
  if([self.filteredListContent count]>0)
  {
    [self.filteredListContent removeAllObjects]; // First clear the filtered array.
  }

  for (int cnt = 0; cnt< [self.listContent count];cnt++)
  {
    NSRange range = [[[self.listContent objectAtIndex:cnt]lowercaseString]  rangeOfString :[searchText lowercaseString]];

    if(range.length>0)
    {
        [self.filteredListContent addObject:[self.listContent objectAtIndex:cnt]];
    }
    self.blSearchBarClicked = TRUE;
    [self.tbViewSearch reloadData];
  }
}
于 2012-11-07T11:57:12.587 回答
0

由于杰克劳伦斯的好建议,我找到了完美的答案:

- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
 {
     NSPredicate *resultPredicate = [NSPredicate 
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                     searchText];

    searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
 }

来源: http: //www.appcoda.com/how-to-add-search-bar-uitableview/

于 2012-11-07T13:51:40.303 回答