Ios UISearchBar在键入文本时不显示任何结果
当我的用户在我的UISearchBar中搜索一个项目时,如果输入到该栏中的完整单词与其中一个结果匹配,则会显示结果例如如果键入“Panda”,Panda将在tableView结果中弹出。但是,如果键入“Pan”,则不会显示任何结果。当用户键入时,如何使搜索结果过滤器工作?即使只输入了“pan”,也应该显示Panda 我的筛选代码当前如下所示: .mIos UISearchBar在键入文本时不显示任何结果,ios,objective-c,Ios,Objective C,当我的用户在我的UISearchBar中搜索一个项目时,如果输入到该栏中的完整单词与其中一个结果匹配,则会显示结果例如如果键入“Panda”,Panda将在tableView结果中弹出。但是,如果键入“Pan”,则不会显示任何结果。当用户键入时,如何使搜索结果过滤器工作?即使只输入了“pan”,也应该显示Panda 我的筛选代码当前如下所示: .m - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searc
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
}
/*
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
} */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [self.neighbourData count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NetworkTableIdentifier = @"sidebarCell";
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSDictionary *userName = [searchResults objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"first name"]];
NSDictionary *userlast = [searchResults objectAtIndex:indexPath.row];
[[cell lastName] setText:[userlast objectForKey:@"last name"]];
NSDictionary *userBio = [searchResults objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"userbio"]];
NSString *profilePath = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"photo_path"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
NSLog(@"This is profilePath %@",profilePath);
} else {
NSDictionary *userName = [self.neighbourData objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"first name"]];
NSDictionary *userlast = [self.neighbourData objectAtIndex:indexPath.row];
[[cell lastName] setText:[userlast objectForKey:@"last name"]];
NSDictionary *userBio = [self.neighbourData objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"userbio"]];
NSString *profilePath = [[self.neighbourData objectAtIndex:indexPath.row] objectForKey:@"photo_path"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
NSLog(@"This is profilePath %@",profilePath);
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 115;
}
邻居数据日志:
[12663:3559832] This is the neighbourdata (
{
address = "1144 fake street";
city = Las Vegas;
"first name" = Panda;
"last name" = Zoo;
"photo_path" = "none";
}
“filterContentForSearchText”方法应位于UISearchBar的“textDidChange”方法中。您当前在哪里编写此方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
@界面ViewController:UIViewController
@属性(弱、非原子)IBUITableView*tableView;
@属性(强,非原子)IBMOutlet UISearchDisplayController*searchDisplayController;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
self.neighbourData=@[@“苹果”,“应用程序”,“贝尔];
NSUserDefaults*userDefaults=[NSUserDefaults standardUserDefaults];
[self.tableView注册表类:[UITableViewCell类]强制重用标识符:@“filterCell”];
}
-(无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本
{
NSPredicate*resultPredicate=[NSPredicate
predicateWithFormat:@“自身包含[cd]@”,
搜索文本];
self.searchResults=[self.neighbourData filteredarrayingpredicate:resultPredicate];
}
/*
-(BOOL)searchDisplayController:(UISearchDisplayController*)控制器
shouldReloadTableForSearchString:(NSString*)searchString
{
[self-filterContentForSearchText:searchString
范围:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
选择范围按钮索引]]];
返回YES;
} */
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
if(tableView==self.searchDisplayController.searchResultsTableView){
返回[self.searchResults计数];
}否则{
返回[self.neighbourData count];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“filterCell]”;
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“filterCell”];
}
if(tableView==self.searchDisplayController.searchResultsTableView){
cell.textlab.text=[self.searchResults objectAtIndex:indexPath.row];
}否则{
cell.textlab.text=[self.neighturdata objectAtIndex:indexPath.row];
}
返回单元;
@结束
您需要在数据过滤后重新加载tableview,并删除searchResult数组的exist对象
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSPredicate * predicate =[NSPredicate predicateWithFormat:@"first name = %@", searchText];
searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
[tableView reloadData];
}
试试这个
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSPredicate * predicate =[NSPredicate predicateWithFormat:@"%K contains[cd] %@",@"first name", searchText];
searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
if searchResults.count == 0
{
NSPredicate * predicate =[NSPredicate predicateWithFormat:@"%K contains[cd] %@",@"last name",searchText];
searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
}
// add predicates for other keys also if you want
[tableView reloadData];
}
建议:
- 避免键的单词之间出现空格('firstName'不推荐使用,firstName'推荐使用)
- 并以小写形式保存值('Panda','Zoo'最好保存为'Panda','Zoo'这将使搜索更简单)
NSPredicate* p = [NSPredicate predicateWithBlock:
^BOOL(id obj, NSDictionary *d) {
NSString* s = obj;
NSStringCompareOptions options = NSCaseInsensitiveSearch;
return ([s rangeOfString:sbc.searchBar.text
options:options].location != NSNotFound);
}];
self.filteredStates = [states filteredArrayUsingPredicate:p];
同时检查这个
希望有帮助。请参阅代码编辑。如果是这种情况,我的BOOL应该更改为什么?因为更改您建议的行会导致BOOL向我抛出错误。好的。您正在使用UISearchDisplayController(在ios8中不推荐使用它。另一种选择是UISearchController)。请尝试注释此代码。如果你的应用程序不支持
NSPredicate* p = [NSPredicate predicateWithBlock:
^BOOL(id obj, NSDictionary *d) {
NSString* s = obj;
NSStringCompareOptions options = NSCaseInsensitiveSearch;
return ([s rangeOfString:sbc.searchBar.text
options:options].location != NSNotFound);
}];
self.filteredStates = [states filteredArrayUsingPredicate:p];