我有一个带有搜索栏的表格视图,我想在文本为空时进行搜索。
我想显示所有数据,当搜索文本为空时(当用户没有输入任何搜索文本时)
注意 - 一切正常,输入搜索文本时,问题仅在搜索文本为空时
不幸的self.arForSearch
是,当搜索文本为空时,不使用数组的内容。
我什至尝试用替换文本,"p"
但未加载数据。
问题 当用户没有输入搜索字符串时,如何显示所有数据?
代码粘贴在下面:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if(searchString.length == 0)
{
searchString = @"p";
NSLog(@"string was empty but was replaced by p");
}
[self filterContentForSearchText:searchString];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)filterContentForSearchText:(NSString*)str{
// for inCaseSensitive search
str = [str uppercaseString];
NSMutableArray *ar=[NSMutableArray array];
for (NSDictionary *d in self.arForTable) {
NSString *strOriginal = [d valueForKey:@"name"];
// for inCaseSensitive search
strOriginal = [strOriginal uppercaseString];
if([strOriginal hasPrefix:str]) {
[ar addObject:d];
}
}
self.arForSearch=[NSArray arrayWithArray:ar];
NSLog(@"self.arForSearch.count = %i", self.arForSearch.count);
}