Iphone UISearchDisplayController searchResultsTableView未隐藏在iOS7中
我正在使用UISearchDisplayController在tableview中显示搜索结果。如果搜索结果为空,则隐藏UISearchDisplaycontroller的searchResultTableView。在iOS 6.0之前都可以正常工作,但在iOS 7中就不行了。 我正在努力寻找一些解决办法,但不幸的是,我还没有找到。 我使用以下语句隐藏SearchResultableView self.searchDisplayController.searchResultsTableView.hidden=是 以下是iOS 6和iOS 7中的屏幕截图 编辑: 我正在使用以下UISearchDisplayController委托方法Iphone UISearchDisplayController searchResultsTableView未隐藏在iOS7中,iphone,ios,objective-c,ios7,xcode5,Iphone,Ios,Objective C,Ios7,Xcode5,我正在使用UISearchDisplayController在tableview中显示搜索结果。如果搜索结果为空,则隐藏UISearchDisplaycontroller的searchResultTableView。在iOS 6.0之前都可以正常工作,但在iOS 7中就不行了。 我正在努力寻找一些解决办法,但不幸的是,我还没有找到。 我使用以下语句隐藏SearchResultableView self.searchDisplayController.searchResultsTableView.
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text]
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:searchOption]];
return YES;
}
-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope{
[Appdelegate.arrFilteredDrugSummary removeAllObjects];
[Appdelegate.arrFilteredDrugID removeAllObjects];
for (DrugDetails *drug in Appdelegate.arrDrugSummary)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(SELF BEGINSWITH [cd] %@)", searchText];
if(![drug.tradeName isEqual:[NSNull null]])
{
[drug.tradeName compare:searchText options:NSCaseInsensitiveSearch];
BOOL resultTradeName;
if((resultTradeName = [predicate evaluateWithObject:drug.tradeName]))
{
if (![Appdelegate.arrFilteredDrugID containsObject:drug.ID])
{
[Appdelegate.arrFilteredDrugSummary addObject:drug];
[Appdelegate.arrFilteredDrugID addObject:drug.ID];
}
}
}
}
if (![Appdelegate.arrFilteredDrugSummary count])
{
[self.btnAddNewDrug setHidden:NO];
self.tblview.hidden=YES;
self.searchDisplayController.searchResultsTableView.hidden = YES;
}
else
{
[self.btnAddNewDrug setHidden:YES];
self.tblview.hidden=NO;
self.searchDisplayController.searchResultsTableView.hidden = NO;
}
[self.searchDisplayController.searchResultsTableView reloadData];
}
我也面临同样的问题。你可以通过这种方式克服它
- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
self.searchDisplayController.searchResultsTableView.hidden = YES;
}
如果发现任何结果,则使用此代码关闭searchDisplayController
if (yourArray.count != 0)
{
// reload your table here
[self.searchDisplayController setActive:NO animated:YES];
self.searchDisplayController.searchResultsTableView.hidden = YES;
}
或
如果搜索结果找到,您可以使用此代码隐藏searchDisplayController
[self searchBarCancelButtonClicked:searchBar];
它将调用UISearchBar searchBarCancelButtonClicked委托和
- (void)searchBarCancelButtonClicked:(UISearchBar *)_searchBar {
if (yourArray.count == 0) {
self.searchDisplayController.searchResultsTableView.hidden = YES;
}
else
{
self.searchDisplayController.searchResultsTableView.hidden = NO;
}
}
我希望它能帮助你 您知道搜索栏是否有焦点
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([_searchBar isFirstResponder]) {
return [_filteredData count];
} else {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
}
设置过滤器参数
-(void)filter:(NSString*)text
{
_filteredData = [[NSMutableArray alloc] init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Entidad" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"titulo" ascending:YES];
NSArray* sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
if(text.length > 0)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(titulo CONTAINS[c] %@)", text];
[fetchRequest setPredicate:predicate];
}
NSError *error;
NSArray* loadedEntities = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
_filteredData = [[NSMutableArray alloc] initWithArray:loadedEntities];
NSLog(@"%@", _filteredData);
[self.tableView reloadData];
}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
[self filter:text];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self filter:@""];
[_searchBar resignFirstResponder];
[_searchBar setText:@""];
[_tableView reloadData];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[_searchBar setShowsCancelButton:YES animated:YES];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[_searchBar setShowsCancelButton:NO animated:YES];
}
我希望能为您服务,我的英语非常有限您把那行代码称为哪里?如果您还没有将其移动到
searchDisplayController:didShowSearchResultsTableView:
,您可能需要尝试将其移动到searchDisplayController:didShowSearchResultsTableView:
。顺便说一句,我在iOS 7上的UISearchDisplayController遇到了太多问题,我只是将其完全剥离出来,编写了完成此项工作所需的自定义代码,并在没有它的情况下与UISearchBar交互。@smileyborg:我编辑了上面的问题,并在实现代码的地方添加了代码,用于隐藏SearchResultableView。请你告诉我哪里出了问题。@smileyborg:我按照你的建议添加了searchDisplayController:didShowSearchResultsTableView:方法。它一个问题解决了,但又遇到了另一个问题。我没有收到iOS 7中“看不见?问我们!”按钮的点击事件。感谢Chandan的回复。。!它在iOS7上工作正常吗。实际上我无法解决这个问题,所以我删除了UISearchDisplayController并实现了UISearchBar委托方法来完成我的任务。我一定会按照你的建议尝试一下。很抱歉我回复晚了,但我现在面临这个问题。是的,Mahesh,它在这里工作得很好。如果您使用的是UISearch栏,那么您可以强制完全调用cancel searchBarCancelButtonClicked degegate并执行您想要的任何操作。
-(void)filter:(NSString*)text
{
_filteredData = [[NSMutableArray alloc] init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Entidad" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"titulo" ascending:YES];
NSArray* sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
if(text.length > 0)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(titulo CONTAINS[c] %@)", text];
[fetchRequest setPredicate:predicate];
}
NSError *error;
NSArray* loadedEntities = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
_filteredData = [[NSMutableArray alloc] initWithArray:loadedEntities];
NSLog(@"%@", _filteredData);
[self.tableView reloadData];
}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
[self filter:text];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self filter:@""];
[_searchBar resignFirstResponder];
[_searchBar setText:@""];
[_tableView reloadData];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[_searchBar setShowsCancelButton:YES animated:YES];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[_searchBar setShowsCancelButton:NO animated:YES];
}