ios搜索栏范围按钮不会更新结果tableview

时间:2013-01-22 10:13:42

标签: ios uisearchbar uisearchdisplaycontroller

我正在尝试在我的表格视图上实现搜索栏。当用户输入文本时,过滤效果很好。当搜索栏中有文本时,范围按钮也可以工作(它通过文本过滤过滤结果)。我的问题是当搜索栏中没有文字时,范围按钮不起作用。过滤方法成功过滤数组;但是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更新但在范围栏更改后它没有更新。

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。我不再使用searchDisplayController,而是使用textDidChangeselectedScopeButtonIndexDidChange。而不是使用两个表视图(正常和self.searchDisplayController.searchResultsTableView)我正在使用单表视图,我改变其数据源,现在似乎这解决了我的问题。但仍然无法弄清楚为什么shouldReloadTableForSearchScope:没有调用cellForRowAtIndexPath:并更新我的表格视图