我的应用程序在用户将其输入到UISearchBar后立即解析搜索结果。一切正常。但现在我想搜索全文。结果在我的NSMutableArray中,但不显示。我认为问题是这段代码:
NSRange nameRange = [SDPE.title rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch];
我该怎么办?
答案 0 :(得分: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];
}
}
答案 1 :(得分: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/