我正在尝试在我的表视图上实现搜索栏.当用户输入文本时,过滤效果很好.当搜索栏中有文本时,范围按钮也可以工作(它通过文本过滤过滤结果).我的问题是当搜索栏中没有文字时,范围按钮不起作用.过滤方法成功过滤数组;但是tableview没有更新.我试过;
searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
但它不起作用.我正在使用
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { // Tells the table data source to reload when text changes [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; // Return YES to cause the search result table view to be reloaded. return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { // Tells the table data source to reload when scope bar selection changes [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; // Return YES to cause the search result table view to be reloaded. return YES;
}
用于文本过滤.我也尝试重新加载数据,它也没有工作.我无法弄清楚为什么在文本更改后tableview更新但在范围栏更改后它没有更新.
解决方法
我解决了这个问题.我不再使用searchDisplayController,而是使用textDidChange和selectedScopeButtonIndexDidChange.而不是使用两个表视图(normal和self.searchDisplayController.searchResultsTableView)我正在使用单表视图并且我更改其数据源,现在看来这解决了我的问题.但仍然无法弄清楚为什么应该调用divoadTableForSearchScope:不调用cellForRowAtIndexPath:并更新我的表视图